android - roboelectric 中工作经理的问题
问题描述
我在我的应用程序中使用自定义工作管理器并禁用工作管理器的默认初始化。但是当我用 roboelectric 测试写测试时。它导致在第二次测试中崩溃。这是我的测试用例。
@Before
fun initDb() {
val context = InstrumentationRegistry.getInstrumentation().context
val config = Configuration.Builder()
// Set log level to Log.DEBUG to make it easier to debug
.setMinimumLoggingLevel(Log.DEBUG)
// Use a SynchronousExecutor here to make it easier to write tests
.setExecutor(SynchronousExecutor())
.build()
// Initialize WorkManager for instrumentation tests.
WorkManagerTestInitHelper.initializeTestWorkManager(context, config)
currencyDatabase = Room.inMemoryDatabaseBuilder(
InstrumentationRegistry.getInstrumentation().targetContext, CurrencyDatabase::class.java
)
.setTransactionExecutor(Executors.newSingleThreadExecutor())
.setQueryExecutor(Executors.newSingleThreadExecutor())
.build()
System.out.println("FUCK")
currencyDao = currencyDatabase.currencyDao()
timestampDao = currencyDatabase.timestampDao()
localRepository = LocalRepository(currencyDao, timestampDao)
}
@Test
fun insertCurrency() {
val currency = Currency(
flag = 1211, exchangeRate = "1200", currencyName = "Dollar", timestamp = "1137187182192", shortName = "USD"
)
val currency1 = Currency(
flag = 1211, exchangeRate = "1200", currencyName = "Dollar", timestamp = "1137187182192", shortName = "USD"
)
testCoroutineScope.launch {
try {
localRepository.saveCurrency(currency)
localRepository.saveCurrency(currency1)
val testObserver: Observer<List<Currency>> = mock()
currencyDao.getCurrency().observeForever(testObserver)
val listClass = ArrayList::class.java as Class<ArrayList<Currency>>
val argumentCaptor = ArgumentCaptor.forClass(listClass)
// 4
verify(testObserver).onChanged(argumentCaptor.capture())
// 5
val capturedArgument = argumentCaptor.value
Log.w("SHIT :: ", capturedArgument.toString())
System.out.println(capturedArgument.toString())
assertTrue(capturedArgument.containsAll(listOf(currency, currency1)))
} catch(e: Exception) {
System.out.println("FUCK :: " + e.toString())
}
}
}
@Test
fun insertTimestamp() {
val timestamp = Timestamp(timestamp = "23242424242")
testCoroutineScope.launch {
try {
localRepository.saveTimestamp(timestamp)
val timestampObserver: Observer<List<Timestamp>> = mock()
localRepository.getTimestamp().observeForever(timestampObserver)
val listClass = ArrayList::class.java as Class<ArrayList<Timestamp>>
val argumentCaptor = ArgumentCaptor.forClass(listClass)
verify(timestampObserver).onChanged(argumentCaptor.capture())
val capturedArgument = argumentCaptor.value
System.out.println(capturedArgument[0].timestamp + " :: ")
assertTrue(capturedArgument[0].timestamp == timestamp.timestamp)
} catch (e: Exception) {
System.out.println(e.toString())
}
}
}
@After
fun closeDb() {
currencyDatabase.close()
}
这是我的自定义工作管理器初始化。
WorkManager.initialize(this, Configuration.Builder().setWorkerFactory(myWorkerFactory).build())
初始化有什么问题?
这是我在第二次测试中遇到的错误。
java.lang.IllegalStateException: WorkManager is already initialized. Did you try to initialize it manually without disabling WorkManagerInitializer? See WorkManager#initialize(Context, Configuration) or the class level Javadoc for more information.
请在这件事上给予我帮助
解决方案
推荐阅读
- python - 在python中创建配对元素列表
- arrays - 根据tableView结果改变标签文字、文字颜色、文字大小
- c++ - 表达式在 C++ 向量中必须具有类类型
- java - 具有以固定速率运行的可运行对象的 java 项目可以在一段时间后停止吗?我的大约40小时后一直冻结
- json - Swift JSON 解析问题,无法返回正确数量的单元格
- combobox - 如何将组合框设置为默认值
- angular - 使用 set 和 get 绑定到属性不会与范围滑块一起更新?
- javascript - 如何取消订阅数组
- android - 我们是否应该在 Android 的 XML 中始终使用 xml version="1.0" 和 encoding="utf-8" ?
- compression - 如何在 Kafka 生产者中添加压缩