问题描述
我无法在不需要robolectric的情况下创建单元测试.我在代码中使用AndroidThreeTen.init(this),如果我禁用了robolectric,则在运行测试时出现错误:org.threeten.bp.zone.ZoneRulesException: No time-zone data files registered
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
,如果我将其保持启用状态,则会显示以下内容:[Robolectric] com.mycomp.,yapp.utilities.log.LogTest.on Calling function w it returns an Int: sdk=28; resources=BINARY
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
我尝试使用testImplementation‘com.jakewharton.threetenabp:threetenabp:1.1.0’没什么区别.我在应用程序和testApplication中调用了AndroidThreeTen.init(this).有任何想法吗?这是我的考验
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))
}
还是因为这个原因我必须使用robolectric吗?(附带说明:日志不是来自android的util.log,而是我自己的类)(已编辑)
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)
推荐答案
JVM单元测试不在Android运行时上运行.代替ThreeTenABP,您可以直接使用ThreeTenBP来为常规JVM初始化相同的API.
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.
在我的项目build.gradle中,我使用类似以下的设置:
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}"
}
其中
threetenabpVersion = '1.2.0'
threetenbpVersion = '1.3.8'
这通常通过ThreeTenABP使用ThreeTenBP,但是在单元测试配置中,它直接将TreeTenBP作为依赖项及其初始化代码添加.我不记得为什么要输入exclude
规则了.已经有几年了.
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.
这篇关于如果没有robolectric,AndroidThreeTen无法在单元测试中工作吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!