android - AndroidThreeTen 在没有 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,而是我自己的类)(已编辑)
解决方案
JVM 单元测试不在 Android 运行时运行。除了 ThreeTenABP,您可以直接使用 ThreeTenBP 为常规 JVM 初始化相同的 API。
在我的项目 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
规则;已经有几年这样的情况了。
推荐阅读
- javascript - 如何在 Google 图表中获取 DatatTable
- excel - 循环遍历可变范围的单元格并用特定字符填充空单元格
- cordova - 添加平台时出现离子框架错误
- javascript - 地址自动填充在网站中不起作用
- ios - 限制情节提要中的警告
- sql-server - Docker 容器可以在重启之间保持状态吗?
- npm - Webpack 文件加载器删除 url 路径中的引号
- python - AttributeError:“员工”对象没有属性“工作时间”
- c# - 如何获取多个设备的位置?
- angular - 使用 OpenLayers 和 Angular 6 向地图添加标记