gpt4 book ai didi

android - 你能对使用 Hilt 构造函数注入(inject)的 Android 工作人员进行单元测试吗

转载 作者:行者123 更新时间:2023-12-03 18:59:52 28 4
gpt4 key购买 nike

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

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。
他们因这个异常(exception)而失败:-
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$1.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$1.evaluate(TestWatcher.java:61)
at org.junit.runners.ParentRunner$3.evaluate(ParentRunner.java:306)
at org.robolectric.RobolectricTestRunner$HelperTestRunner$1.evaluate(RobolectricTestRunner.java:575)
at org.robolectric.internal.SandboxTestRunner$2.lambda$evaluate$0(SandboxTestRunner.java:263)
at org.robolectric.internal.bytecode.Sandbox.lambda$runOnMainThread$0(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 yourproduction code. Testing with Hilt requires no maintenance becauseHilt automatically generates a new set of components for each test.


我究竟做错了什么?
我需要更改什么以使我的纯单元测试能够创建 Android 实例 CoroutineWorker采用 Hilt 构造函数注入(inject)?
更新
我的 UnitTest 类似于:-
class MyWorkerTest : BaseTest() {

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

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

}
}
}
我的 BaseTest 类(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 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必须在 Worker 中测试正确的调用和行为的 doWork() . TestListenableWorkerBuilder用于仪器测试。还建议在单元测试中避免使用 hilt 和 dagger。

关于android - 你能对使用 Hilt 构造函数注入(inject)的 Android 工作人员进行单元测试吗,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65128121/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com