首页 > 解决方案 > 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.

请在这件事上给予我帮助

标签: androidkotlintestingrobolectricandroid-workmanager

解决方案


推荐阅读