Неразрешенная ссылка: TestCoroutineDispatcher

#unit-testing #android-room #kotlin-coroutines

#модульное тестирование #android-комната #kotlin-сопрограммы

Вопрос:

Я пытаюсь протестировать вставку и выборку базы данных room с помощью TestCoroutineDispatchers и runBlockingTest. Поскольку мне нужен контекст для создания экземпляра базы данных, я попытался выполнить тест в androidTest, но я получаю сообщение об ошибке ниже. Может ли кто-нибудь, пожалуйста, помочь мне с решением.

Я следую инструкциям, написанным по этой ссылке следующим образом https://medium.com/@eyalg/testing-androidx-room-kotlin-coroutines-2d1faa3e674f

«Неразрешенная ссылка: TestCoroutineDispatcher» «Неразрешенная ссылка: TestCoroutineScope» «Неразрешенная ссылка: 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)
    }
}
 

И я использую приведенные ниже зависимости :

 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"
 

Ответ №1:

Вы объявили testImplementation 'org.jetbrains.kotlinx:kotlinx-coroutines-test:1.4.2' для своих модульных тестов.

Но если вы хотите использовать TestCoroutineDispatcher for androidTest, вы также должны объявить его зависимость : androidTestImplementation 'org.jetbrains.kotlinx:kotlinx-coroutines-test:1.4.2' .

То же самое для других зависимостей, которые вы хотите использовать для androidTest.