AndroidThreeTen没有在没有robolectric的单元测试中工作?

问题描述 投票:1回答:1

我无法在不需要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? (旁注:Log不是来自android的util.log,而是我自己的类)(已编辑)

android unit-testing testing kotlin robolectric
1个回答
7
投票

JVM单元测试不在Android运行时上运行。您可以直接使用ThreeTenBP来为常规JVM初始化相同的API,而不是ThreeTenABP。

在我的项目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作为依赖项添加其init代码。不记得为什么我放入exclude规则;已经好几年了。

© www.soinside.com 2019 - 2024. All rights reserved.