如何在不引发 org.threeten.bp.zone.ZoneRulesException

我有以下应用程序代码。


应用代码

public static long advanceByMax1HourWithoutOverflow(long currentTimeMillis) {

    ZoneId zoneId = ZoneId.systemDefault();


    ZonedDateTime zonedDateTime = Instant.ofEpochMilli(currentTimeMillis).atZone(zoneId);


    // 0-23

    int hour = zonedDateTime.getHour();


    if (hour < 23) {

        zonedDateTime = zonedDateTime.plusHours(1);

        return zonedDateTime.toInstant().toEpochMilli();

    }


    // 0-59

    int minute = zonedDateTime.getMinute();


    if (minute < 59) {

        zonedDateTime = zonedDateTime.plusMinutes(1);

        return zonedDateTime.toInstant().toEpochMilli();

    }


    return currentTimeMillis;

}

我用以下内容为它编写了一个单元测试。


单元测试代码

@Test

public void advanceByMax1HourWithoutOverflow() {

    /*

    27 October 2018

    1540648800000 -> 1540652400000

    22:00 -> 23:00

    */

    long currentTimeMillis = 1540648800000L;

    long expected = 1540652400000L;

    long output = Utils.advanceByMax1HourWithoutOverflow(currentTimeMillis);

    assertEquals(expected, output);

我收到以下错误


org.threeten.bp.zone.ZoneRulesException: No time-zone data files registered

我试图通过使用来修复它


单元测试代码

@Test

public void advanceByMax1HourWithoutOverflow() {

    Context context = mock(Context.class);

    AndroidThreeTen.init(context);


    /*

    27 October 2018

    1540648800000 -> 1540652400000

    22:00 -> 23:00

    */

    long currentTimeMillis = 1540648800000L;

    long expected = 1540652400000L;

    long output = Utils.advanceByMax1HourWithoutOverflow(currentTimeMillis);

    assertEquals(expected, output);

我越来越


java.lang.ExceptionInInitializerError

    at org.threeten.bp.ZoneRegion.ofId(ZoneRegion.java:143)

    at org.threeten.bp.ZoneId.of(ZoneId.java:358)

    at org.threeten.bp.ZoneId.of(ZoneId.java:286)

    at org.threeten.bp.ZoneId.systemDefault(ZoneId.java:245)

有什么方法可以通过单元测试,而无需更改当前应用程序的方法签名?我只想保留我当前的应用程序方法签名而不做任何修改。


long advanceByMax1HourWithoutOverflow(long currentTimeMillis)



慕田峪7331174
浏览 203回答 1
1回答

弑天下

ThreeTen-Android Backport 向不支持 Java 8 的 Android 系统提供了 Java SE 8 日期时间类的向后移植。 这个问题的原因是单元测试在 JVM 上运行,而不是 Android开发者所说的:仅在本地计算机上运行的单元测试。这些测试被编译为在 Java 虚拟机 (JVM) 上本地运行,以最大限度地减少执行时间。如果您的测试依赖于 Android 框架中的对象,我们建议使用 Robolectric。对于依赖于您自己的依赖项的测试,请使用模拟对象来模拟您的依赖项的行为。Jake Wharton(ThreeTen-Android Backport 所有者)关于没有通过 Robolectric 建立资源说:这是一个 Robolectric 错误。要么向他们报告,要么切换到在单元测试中使用普通的 ThreeTenBP。或两者!因此,我们应该使用原始 ThreeTen Backport 库来解决这个问题。所以我们应该将它的依赖添加到build.gradle单元测试要使用的文件中。在 build.gradle 文件中:dependencies {&nbsp; &nbsp; implementation 'com.jakewharton.threetenabp:threetenabp:1.1.1'&nbsp; &nbsp; testImplementation ('org.threeten:threetenbp:1.3.2'){&nbsp; &nbsp; &nbsp; &nbsp; exclude group:'com.jakewharton.threetenabp', module:'threetenabp'&nbsp; &nbsp; }}在单元测试类中:@Testpublic void advanceByMax1HourWithoutOverflow() {&nbsp; &nbsp; /*&nbsp; &nbsp; 27 October 2018&nbsp; &nbsp; 1540648800000 -> 1540652400000&nbsp; &nbsp; 22:00 -> 23:00&nbsp; &nbsp; */&nbsp; &nbsp; long currentTimeMillis = 1540648800000L;&nbsp; &nbsp; long expected = 1540652400000L;&nbsp; &nbsp; long output = Utils.advanceByMax1HourWithoutOverflow(currentTimeMillis);&nbsp; &nbsp; assertEquals(expected, output);}结果:
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java