AndroidThreeTen 在没有 robolectric 的情况下无法在单元测试中工作?

AndroidThreeTen not working in unit test without robolectric?

我在创建不需要 robolectric 的单元测试时遇到了问题。我在我的代码中使用 AndroidThreeTen.init(this) 并且当我 运行 我的测试如果我禁用 robolectric 我得到一个错误: org.threeten.bp.zone.ZoneRulesException: No time-zone data files registered

如果我启用它,我会得到这个: [Robolectric] com.mycomp.,yapp.utilities.log.LogTest.on Calling function w it returns an Int: sdk=28; resources=BINARY

我试过使用 testImplementation ‘com.jakewharton.threetenabp:threetenabp:1.1.0’ 没有区别。我在我的应用程序和 testApplication 中调用了 AndroidThreeTen.init(this)。有任何想法吗? 这是我的测试

@Test
    fun `on Calling function i it returns an Int`() {
        assertThat("Returned class is not an Int", Log.i("Test", "Test"), isA(Int::class.java))
        assertThat("Returned Int is not 0", Log.i("Test", "Test"), `is`(0))
    }

或者我是否因此必须使用 robolectric? (旁注:日志不是来自 android 的 util.log,而是我自己的 class)(已编辑)

JVM 单元测试不会 运行 在 Android 运行 时间。除了 ThreeTenABP,您可以直接使用 ThreeTenBP 来获得相同的 API 为常规 JVM 初始化。

在我的项目中 build.gradle 我使用的设置如下:

implementation "com.jakewharton.threetenabp:threetenabp:${threetenabpVersion}"
testImplementation("org.threeten:threetenbp:${threetenbpVersion}") {
    exclude module: "com.jakewharton.threetenabp:threetenabp:${threetenabpVersion}"
}

哪里

threetenabpVersion = '1.2.0'
threetenbpVersion = '1.3.8'

这通常通过 ThreeTenABP 使用 ThreeTenBP,但在单元测试配置中,它直接添加 TreeTenBP 作为依赖项及其初始化代码。不记得我为什么要加入 exclude 规则;好几年了。