AndroidThreeTen not working in unit test without robolectric?

邮差的信 提交于 2020-07-20 21:00:07

问题


I'm having trouble creating a unit test without needing robolectric. I am using AndroidThreeTen.init(this) in my code and when I run my test if I disable robolectric I get an error: org.threeten.bp.zone.ZoneRulesException: No time-zone data files registered

and if I leave it enabled I get this: [Robolectric] com.mycomp.,yapp.utilities.log.LogTest.on Calling function w it returns an Int: sdk=28; resources=BINARY

I have tried using testImplementation ‘com.jakewharton.threetenabp:threetenabp:1.1.0’ made no difference. I have AndroidThreeTen.init(this) called in my application and testApplication. any ideas? this is my test

@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))
    }

Or do I have to use robolectric because of this? (Side note: Log is not the util.log from android but my own class) (edited)


回答1:


JVM unit tests don't run on Android runtime. Instead of ThreeTenABP, you can just use ThreeTenBP directly to get the same API initialised for a regular JVM.

In my project build.gradle I use a setup like:

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

where

threetenabpVersion = '1.2.0'
threetenbpVersion = '1.3.8'

This uses ThreeTenBP via ThreeTenABP normally, but in unit test configuration it adds TreeTenBP directly as a dependency, with its init code. Cannot remember exactly why I put in the exclude rule; it's been like that for a few years already.



来源:https://stackoverflow.com/questions/55340569/androidthreeten-not-working-in-unit-test-without-robolectric

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!