您可以使用 Hilt 构造函数注入的 UnitTest Android 工作者吗

Can you UnitTest Android workers that employ Hilt constructor injection

我正在调查 Hilt 在我当前的 Android 应用程序中的使用情况。

api 'androidx.hilt:hilt-work:1.0.0-alpha02'
implementation "com.google.dagger:hilt-android:2.30.1-alpha"
kapt 'com.google.dagger:hilt-android-compiler:2.30.1-alpha'
kapt 'androidx.hilt:hilt-compiler:1.0.0-alpha02'

api "androidx.work:work-runtime:2.4.0"
implementation "androidx.work:work-runtime-ktx:2.4.0"

testImplementation "androidx.work:work-testing:2.4.0"

testImplementation 'com.google.dagger:hilt-android-testing:2.30.1-alpha'
kaptTest 'com.google.dagger:hilt-android-compiler:2.30.1-alpha'

testImplementation "org.jetbrains.kotlinx:kotlinx-coroutines-test:1.3.7"
testImplementation "androidx.arch.core:core-testing:2.1.0"
testImplementation "junit:junit:4.13.1"
testImplementation "org.robolectric:robolectric:4.4"
testImplementation 'io.mockk:mockk:1.10.3'
testImplementation "androidx.test:core:1.3.0"
testImplementation "androidx.test.ext:junit:1.1.2"

不过我无法让我的 androidx.work.CoroutineWorker UnitTest 达到 运行。

他们因以下异常而失败:-

java.lang.IllegalStateException: Could not create an instance of ListenableWorker com.my.manager.background.work.worker.MyWorker

    at androidx.work.testing.TestListenableWorkerBuilder.build(TestListenableWorkerBuilder.java:361)
    at com.my.manager.background.work.ApplicationFeaturesWorkerTest.testApplicationFeaturesWorker(MyWorkerTest.kt:13)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:566)
    at org.junit.runners.model.FrameworkMethod.runReflectiveCall(FrameworkMethod.java:59)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:56)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
    at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
    at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)
    at org.junit.rules.TestWatcher.evaluate(TestWatcher.java:61)
    at org.junit.runners.ParentRunner.evaluate(ParentRunner.java:306)
    at org.robolectric.RobolectricTestRunner$HelperTestRunner.evaluate(RobolectricTestRunner.java:575)
    at org.robolectric.internal.SandboxTestRunner.lambda$evaluate[=12=](SandboxTestRunner.java:263)
    at org.robolectric.internal.bytecode.Sandbox.lambda$runOnMainThread[=12=](Sandbox.java:89)
    at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264)
    at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
    at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
    at java.base/java.lang.Thread.run(Thread.java:834)

我的 Worker 构造函数类似于:-

class MyWorker @WorkerInject constructor(@Assisted context: Context, @Assisted params: WorkerParameters, private val service: MyApi) : BaseWorker(context, params) {
}

Hilt 文档指出:-

End-to-end tests

For integration tests, Hilt injects dependencies as it would in your production code. Testing with Hilt requires no maintenance because Hilt automatically generates a new set of components for each test.

我做错了什么?

我需要更改什么才能使我的纯单元测试能够创建使用 Hilt 构造函数注入的 Android CoroutineWorker 实例?

更新

我的单元测试类似于:-

class MyWorkerTest : BaseTest() {

    @Test
    fun testMyWorker() {
        val worker = TestListenableWorkerBuilder<MyWorker>(mContextMock).build()
        runBlocking {
            val result = worker.doWork()

            assert(result == ListenableWorker.Result.success())

        }
    }
}

我的基础测试class:-

@RunWith(AndroidJUnit4::class)
@Config(manifest = Config.NONE, sdk = [O, O_MR1, P, Q])
abstract class BaseTest {

    lateinit var executor: Executor

    @get:Rule
    val instantTaskExecutorRule = InstantTaskExecutorRule()

    val mContextMock: Application = ApplicationProvider.getApplicationContext()

    @Before
    fun setup() {

        executor = Executors.newSingleThreadExecutor()
    }

    fun manufactureClient(): OkHttpClient {

        return OkHttpClient.Builder()
            .connectTimeout(OK_HTTP_CLIENT_TIMEOUT, TimeUnit.SECONDS)
            .readTimeout(OK_HTTP_CLIENT_TIMEOUT, TimeUnit.SECONDS)
            .writeTimeout(OK_HTTP_CLIENT_TIMEOUT, TimeUnit.SECONDS)
            .callTimeout(OK_HTTP_CLIENT_TIMEOUT, TimeUnit.SECONDS)
            .followSslRedirects(true)
            .retryOnConnectionFailure(true)
            .followRedirects(true)
            .addInterceptor(HttpLoggingInterceptor().apply {
                level = HttpLoggingInterceptor.Level.BASIC
            }).build()
    }

    @After
    fun tearDown() {

    }
}

如果您需要对其进行单元测试,那么您可能不需要使用 TestListenableWorkerBuilder 来实例化 Worker,而是将它们实例化为任何其他 class.

class MyWorkerTest : BaseTest() {
    private lateinit var worker : MyWorker 
     
    @Before
    fun setupWorker(){ 
        worker = MyWorker(mockContext, mockOtherClass)
    } 
    @Test
    fun testMyWorker() = runBlocking {
       val result = worker.doWork()
       verify { mockOtherClass.someFunction() }
       //other assertions
      Unit 
    }
}

单元测试 Worker 必须测试 WorkerdoWork().

中的正确调用和行为

TestListenableWorkerBuilder 用于仪器测试。单元测试也建议避免使用 hilt 和 dagger。

根据 Nikola 的建议并参考 Google Codelab,您可以使用 WorkManagerTestRule 创建自己的 WorkParameters,如下所示

@get:Rule
var wmRule = WorkManagerTestRule()

...

val configuration = wmRule.configuration
val executor = configuration.executor
val workManagerTaskExecutor = WorkManagerTaskExecutor(executor)
val workDatabase = WorkDatabase.create(
    context,
    workManagerTaskExecutor.backgroundExecutor,
    true
)

// you can customize your UUID, input data based on your scenario
val workParameters = WorkerParameters(
    UUID.fromString("d1e5a17a-bed4-11ec-9d64-0242ac120002"),
    Data.EMPTY,
    listOf(),
    WorkerParameters.RuntimeExtras(),
    1,
    executor,
    workManagerTaskExecutor,
    configuration.workerFactory,
    WorkProgressUpdater(workDatabase, workManagerTaskExecutor),
    WorkForegroundUpdater(
        workDatabase,
        object : ForegroundProcessor {
            override fun startForeground(
                 workSpecId: String,
                 foregroundInfo: ForegroundInfo
            ) {

            }

            override fun stopForeground(workSpecId: String) {

            }
        },
        workManagerTaskExecutor
   ))