Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/kotlin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android roboelectric中workmanager的问题_Android_Kotlin_Testing_Robolectric_Android Workmanager - Fatal编程技术网

Android roboelectric中workmanager的问题

Android roboelectric中workmanager的问题,android,kotlin,testing,robolectric,android-workmanager,Android,Kotlin,Testing,Robolectric,Android Workmanager,我正在应用程序中使用自定义workmanager,并禁用了workmanager的默认初始化。但当我用roboelectric测试写测试时。它导致在第二次测试中崩溃。这是我的测试用例 @Before fun initDb() { val context = InstrumentationRegistry.getInstrumentation().context val config = Configuration.Builder()

我正在应用程序中使用自定义workmanager,并禁用了workmanager的默认初始化。但当我用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()
    }
@之前
fun initDb(){
val context=InstrumentationRegistry.getInstrumentation().context
val config=Configuration.Builder()
//将日志级别设置为log.DEBUG以使调试更容易
.setMinimumLoggingLevel(Log.DEBUG)
//在这里使用SynchronousExecutor可以更容易地编写测试
.setExecutor(SynchronousExecutor())
.build()
//初始化检测测试的WorkManager。
WorkManagerTestInitHelper.initializeTestWorkManager(上下文,配置)
currencyDatabase=Room.inMemoryDatabaseBuilder(
InstrumentationRegistry.getInstrumentation().targetContext,CurrencyDatabase::class.java
)
.setTransactionExecutor(Executors.newSingleThreadExecutor())
.setQueryExecutor(Executors.newSingleThreadExecutor())
.build()
System.out.println(“操”)
currencyDao=currencyDatabase.currencyDao()
timestampDao=currencyDatabase.timestampDao()
localRepository=localRepository(currencyDao,timestampDao)
}
@试验
有趣的插入电流(){
val货币=货币(
flag=1211,exchangeRate=“1200”,currencyName=“美元”,timestamp=“1137187182192”,shortName=“美元”
)
val currency1=货币(
flag=1211,exchangeRate=“1200”,currencyName=“美元”,timestamp=“1137187182192”,shortName=“美元”
)
testCoroutineScope.launch{
试一试{
localRepository.saveCurrency(货币)
localRepository.saveCurrency(currency1)
val testObserver:Observer=mock()
currencyDao.getCurrency().observeForever(testObserver)
val listClass=ArrayList::class.java作为类
val argumentCaptor=argumentCaptor.forClass(listClass)
// 4
验证(testObserver).onChanged(argumentCaptor.capture())
// 5
val capturedArgument=argumentCaptor.value
Log.w(“狗屎::”,capturedArgument.toString()
System.out.println(capturedArgument.toString())
assertTrue(capturedArgument.containsAll(列表(货币、币种1)))
}捕获(e:例外){
System.out.println(“FUCK::”+e.toString())
}
}
}
@试验
有趣的insertTimestamp(){
val timestamp=时间戳(timestamp=“2324242”)
testCoroutineScope.launch{
试一试{
localRepository.saveTimestamp(时间戳)
val timestamposerver:Observer=mock()
localRepository.getTimestamp().ObserveForevir(timestampObserver)
val listClass=ArrayList::class.java作为类
val argumentCaptor=argumentCaptor.forClass(listClass)
验证(timestampObserver).onChanged(argumentCaptor.capture())
val capturedArgument=argumentCaptor.value
System.out.println(capturedArgument[0]。时间戳+“:”)
assertTrue(capturedArgument[0]。时间戳==时间戳。时间戳)
}捕获(e:例外){
System.out.println(例如toString())
}
}
}
@之后
(b){
currencyDatabase.close()
}
这是我的自定义workmanager初始化

WorkManager.initialize(这是Configuration.Builder().setWorkerFactory(myWorkerFactory.build())

初始化有什么问题

这是我在第二次测试中得到的错误。
java.lang.IllegalStateException:WorkManager已初始化。您是否尝试在不禁用WorkManagerInitializer的情况下手动初始化它?有关更多信息,请参阅WorkManager#初始化(上下文、配置)或类级Javadoc。

请帮我做这个