mardi 26 mai 2020

Issues with workmanager in roboelectric

I am using custom workmanager in my app and disabled default initialization of workmanager. but when i test write test with roboelectric. It causes to crash in second test. here is my test cases.

@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()
    }

this is my custom workmanager initialization.

WorkManager.initialize(this, Configuration.Builder().setWorkerFactory(myWorkerFactory).build())

What is wrong with initialization?

here is the error i got in second test. 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.

please help me with this

Aucun commentaire:

Enregistrer un commentaire