Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unresolved reference: TestCoroutineDispatcher

I am trying to test room database insertion and fetching with the help of TestCoroutineDispatchers and runBlockingTest. As I need the context for creating a database instance, I tried to achieve the test in androidTest, but I am getting below error. Can any one please help me out with a solution.

I am following the instructions written at this link as follows https://medium.com/@eyalg/testing-androidx-room-kotlin-coroutines-2d1faa3e674f

"Unresolved reference: TestCoroutineDispatcher" "Unresolved reference: TestCoroutineScope" "Unresolved reference: runBlockingTest"

@ExperimentalCoroutinesApi
@RunWith(AndroidJUnit4::class)
class PokemonDatabaseTest {

    private lateinit var pokemonDao : PokemonFavouriteDao
    private lateinit var db : PokemonDatabase


    val testDispatcher = TestCoroutineDispatcher()
    val testScope = TestCoroutineScope(testDispatcher)


    @Before
    fun setUp() {

      db = Room
          .inMemoryDatabaseBuilder(InstrumentationRegistry.getInstrumentation().context, PokemonDatabase::class.java)
          .setTransactionExecutor(testDispatcher.asExecutor())
          .setQueryExecutor(testDispatcher.asExecutor()).build()
        pokemonDao = db.pfDao
    }

    @Test
    fun storeFavouritePokemon()   = runBlockingTest {
        val pokemon = DataFactory.makePokemon()
        assertThat(pokemonDao.getFavouritePokemon(), null)
    }
}

And I am using below dependencies :

implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-core:1.3.9'
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.3.9'
testImplementation 'org.jetbrains.kotlinx:kotlinx-coroutines-test:1.4.2'

androidTestImplementation 'androidx.test:runner:1.1.0'
androidTestImplementation 'androidx.test:rules:1.1.0'
testImplementation 'androidx.test:core:1.2.0'
testImplementation "org.robolectric:robolectric:4.0.2"

def archCoreVersion = '2.1.0'
    def espressoCoreVersion = "3.3.0"
    def espressoVersion = '3.3.0'

espressoCore: "androidx.test.espresso:espresso-core:$espressoCoreVersion",
        espressoContrib : "androidx.test.espresso:espresso-contrib:$espressoVersion",
        espressoIntents : "androidx.test.espresso:espresso-intents:$espressoVersion",
        espressoResource: "androidx.test.espresso:espresso-idling-resource:$espressoVersion",
        coreRuntime : "androidx.arch.core:core-runtime:$archCoreVersion",
        coreTesting : "androidx.arch.core:core-testing:$archCoreVersion"
like image 499
Debin Tom Avatar asked Dec 31 '22 17:12

Debin Tom


1 Answers

You declared testImplementation 'org.jetbrains.kotlinx:kotlinx-coroutines-test:1.4.2' for your unit tests.

But if you want to use TestCoroutineDispatcher for androidTest you have to declare it's dependency as well: androidTestImplementation 'org.jetbrains.kotlinx:kotlinx-coroutines-test:1.4.2'.

Same for other dependencies you wanna use for androidTest's.

like image 134
ChristianB Avatar answered Jan 08 '23 06:01

ChristianB