Я пытаюсь протестировать простой CoroutineWorker, используя AndroidJUnit4 в качестве Официальные документы Упомянул

Мой тест и ошибка:

package com.shalaga44.workmanager

import android.content.Context
import androidx.test.core.app.ApplicationProvider
import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.work.testing.TestListenableWorkerBuilder
import kotlinx.coroutines.runBlocking
import org.hamcrest.Matchers.`is`
import org.junit.Assert.assertThat
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith

@RunWith(AndroidJUnit4::class)
class SleepWorkerTest {
    private lateinit var context: Context
    @Before
    fun setUp() {
        context = ApplicationProvider.getApplicationContext() }
    @Test
    fun testSleepWorker() {
        val worker = TestListenableWorkerBuilder<SleepWorker>(context).build()
        runBlocking {
            val result = worker.doWork()
            assertThat(result, `is`(Result.success())) //The Error here
            //Type inference failed. Please try to specify type arguments explicitly.
        }
    }
}

Мой работник:

package com.shalaga44.workmanager

import android.content.Context
import androidx.work.CoroutineWorker
import androidx.work.WorkerParameters
import kotlinx.coroutines.delay
class SleepWorker(context: Context, parameters: WorkerParameters) :
    CoroutineWorker(context, parameters) {
    override suspend fun doWork(): Result {
        delay(1000)
        return Result.success()
    }
}

Мои зависимости:

dependencies {

    def work_version = "2.3.4"
    def versions_junit = "4.12"
    def versions_atsl_junit = "1.1.1"
    def versions_atsl_runner = "1.2.0"
    def versions_atsl_rules = "1.2.0"
    def versions_espresso = "3.2.0"
    ...

    implementation "androidx.work:work-runtime-ktx:$work_version"
    androidTestImplementation "androidx.work:work-testing:$work_version"
    ...

    testImplementation "junit:junit:$versions_junit"

    androidTestImplementation "androidx.test.ext:junit:$versions_atsl_junit"
    androidTestImplementation "androidx.test:runner:$versions_atsl_runner"
    androidTestImplementation "androidx.test:rules:$versions_atsl_rules"
    androidTestImplementation "androidx.test.espresso:espresso-core:$versions_espresso"
  }
kotlin version = "1.3.72"
android gradle plugin version = "4.0.0"
gradle version =  6.3
Android Studio 4.0
Build #AI-193.6911.18.40.6514223, built on May 20, 2020
Runtime version: 1.8.0_242-release-1644-b3-6222593 amd64
VM: OpenJDK 64-Bit Server VM by JetBrains s.r.o
Linux 5.4.0-40-generic
GC: ParNew, ConcurrentMarkSweep
Memory: 2024M
Cores: 8
Registry: ide.new.welcome.screen.force=true, ide.balloon.shadow.size=0
Non-Bundled Plugins: com.chrisrm.idea.MaterialThemeUI, com.intellij.marketplace
1
Mohamed Moawia 13 Июл 2020 в 15:53

1 ответ

Лучший ответ

Вам не хватает импорта для androidx.work.ListenableWorker.Result, поэтому вы автоматически разрешаете kotlin.Result.

Просто добавьте в начало теста следующее:

import androidx.work.ListenableWorker.Result
2
dlam 14 Июл 2020 в 01:49