java.lang.RuntimeException:缺少从 @HiltAndroidTest 生成的 Hilt 类
java.lang.RuntimeException: Hilt classes generated from @HiltAndroidTest are missing
我正在尝试 运行 使用 Hilt 进行简单的 Robolectric 测试。
@HiltAndroidTest
@Config(application = HiltTestApplication::class)
@RunWith(AndroidJUnit4::class)
class SplashActivityTest2 {
@get:Rule
var hiltRule = HiltAndroidRule(this)
@Test
fun fooTest() {
val splashActivity = ActivityScenario.launch(SplashActivity::class.java)
}
}
但是当我尝试 运行 使用 Android Studio 进行测试时,我得到
java.lang.RuntimeException: Hilt classes generated from
@HiltAndroidTest are missing. Check that you have annotated your test
class with @HiltAndroidTest and that the processor is running over
your test
这是完整的堆栈跟踪。
[Robolectric] com.theapache64.nemo.feature.splash.SplashActivityTest2.fooTest: sdk=28; resources=BINARY
Called loadFromPath(/system/framework/framework-res.apk, true); mode=binary sdk=28
java.lang.RuntimeException: Hilt classes generated from @HiltAndroidTest are missing. Check that you have annotated your test class with @HiltAndroidTest and that the processor is running over your test
at dagger.hilt.android.internal.testing.TestApplicationComponentManager.<init>(TestApplicationComponentManager.java:68)
at dagger.hilt.android.testing.HiltTestApplication.attachBaseContext(HiltTestApplication.java:39)
at android.app.Application.attach(Application.java:212)
at org.robolectric.util.ReflectionHelpers.callInstanceMethod(ReflectionHelpers.java:283)
at org.robolectric.shadows.ShadowApplication.callAttach(ShadowApplication.java:79)
at org.robolectric.android.internal.AndroidTestEnvironment.installAndCreateApplication(AndroidTestEnvironment.java:250)
at org.robolectric.android.internal.AndroidTestEnvironment.setUpApplicationState(AndroidTestEnvironment.java:169)
at org.robolectric.RobolectricTestRunner.beforeTest(RobolectricTestRunner.java:301)
at org.robolectric.internal.SandboxTestRunner.lambda$evaluate[=13=](SandboxTestRunner.java:243)
at org.robolectric.internal.bytecode.Sandbox.lambda$runOnMainThread[=13=](Sandbox.java:89)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at java.lang.Thread.run(Thread.java:748)
Caused by: java.lang.ClassNotFoundException: dagger.hilt.android.internal.testing.TestComponentDataSupplierImpl
at org.robolectric.internal.bytecode.SandboxClassLoader.getByteCode(SandboxClassLoader.java:164)
at org.robolectric.internal.bytecode.SandboxClassLoader.maybeInstrumentClass(SandboxClassLoader.java:119)
at org.robolectric.internal.bytecode.SandboxClassLoader.lambda$findClass[=13=](SandboxClassLoader.java:112)
at org.robolectric.util.PerfStatsCollector.measure(PerfStatsCollector.java:53)
at org.robolectric.internal.bytecode.SandboxClassLoader.findClass(SandboxClassLoader.java:111)
at java.lang.ClassLoader.loadClass(ClassLoader.java:419)
at java.lang.ClassLoader.loadClass(ClassLoader.java:352)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:264)
at dagger.hilt.android.internal.testing.TestApplicationComponentManager.<init>(TestApplicationComponentManager.java:58)
at dagger.hilt.android.testing.HiltTestApplication.attachBaseContext(HiltTestApplication.java:39)
at android.app.Application.$$robo$$android_app_Application$attach(Application.java:212)
at android.app.Application.attach(Application.java)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
... 11 more
Process finished with exit code 255
这是我的 app/build.gradle
具有纯 instrumentationTest (androidTest)
的同一文件仍然有效。
你错过了这个
kaptTest 'com.google.dagger:hilt-android-compiler:2.28-alpha'
来自您的依赖项..
来源:https://developer.android.com/training/dependency-injection/hilt-testing
dependencies {
// For Robolectric tests.
testImplementation 'com.google.dagger:hilt-android-testing:2.28-alpha'
// ...with Kotlin.
kaptTest 'com.google.dagger:hilt-android-compiler:2.28-alpha'
// ...with Java.
testAnnotationProcessor 'com.google.dagger:hilt-android-compiler:2.28-alpha'
// For instrumented tests.
androidTestImplementation 'com.google.dagger:hilt-android-testing:2.28-alpha'
// ...with Kotlin.
kaptAndroidTest 'com.google.dagger:hilt-android-compiler:2.28-alpha'
// ...with Java.
androidTestAnnotationProcessor 'com.google.dagger:hilt-android-compiler:2.28-alpha'
}
使匕首柄与 Robolectric 正常工作。请确保您具备以下条件:
- 从您的依赖项中获得
kaptTest 'com.google.dagger:hilt-android-compiler:...'
。
- 在你的测试中有
@HiltAndroidTest
class。
- 在您的测试 class 中有
@Config(application = HiltTestApplication::class)
或在 robolectric.properties 文件中指定要使用的应用程序:
application=dagger.hilt.android.testing.HiltTestApplication
我正在尝试 运行 使用 Hilt 进行简单的 Robolectric 测试。
@HiltAndroidTest
@Config(application = HiltTestApplication::class)
@RunWith(AndroidJUnit4::class)
class SplashActivityTest2 {
@get:Rule
var hiltRule = HiltAndroidRule(this)
@Test
fun fooTest() {
val splashActivity = ActivityScenario.launch(SplashActivity::class.java)
}
}
但是当我尝试 运行 使用 Android Studio 进行测试时,我得到
java.lang.RuntimeException: Hilt classes generated from @HiltAndroidTest are missing. Check that you have annotated your test class with @HiltAndroidTest and that the processor is running over your test
这是完整的堆栈跟踪。
[Robolectric] com.theapache64.nemo.feature.splash.SplashActivityTest2.fooTest: sdk=28; resources=BINARY
Called loadFromPath(/system/framework/framework-res.apk, true); mode=binary sdk=28
java.lang.RuntimeException: Hilt classes generated from @HiltAndroidTest are missing. Check that you have annotated your test class with @HiltAndroidTest and that the processor is running over your test
at dagger.hilt.android.internal.testing.TestApplicationComponentManager.<init>(TestApplicationComponentManager.java:68)
at dagger.hilt.android.testing.HiltTestApplication.attachBaseContext(HiltTestApplication.java:39)
at android.app.Application.attach(Application.java:212)
at org.robolectric.util.ReflectionHelpers.callInstanceMethod(ReflectionHelpers.java:283)
at org.robolectric.shadows.ShadowApplication.callAttach(ShadowApplication.java:79)
at org.robolectric.android.internal.AndroidTestEnvironment.installAndCreateApplication(AndroidTestEnvironment.java:250)
at org.robolectric.android.internal.AndroidTestEnvironment.setUpApplicationState(AndroidTestEnvironment.java:169)
at org.robolectric.RobolectricTestRunner.beforeTest(RobolectricTestRunner.java:301)
at org.robolectric.internal.SandboxTestRunner.lambda$evaluate[=13=](SandboxTestRunner.java:243)
at org.robolectric.internal.bytecode.Sandbox.lambda$runOnMainThread[=13=](Sandbox.java:89)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at java.lang.Thread.run(Thread.java:748)
Caused by: java.lang.ClassNotFoundException: dagger.hilt.android.internal.testing.TestComponentDataSupplierImpl
at org.robolectric.internal.bytecode.SandboxClassLoader.getByteCode(SandboxClassLoader.java:164)
at org.robolectric.internal.bytecode.SandboxClassLoader.maybeInstrumentClass(SandboxClassLoader.java:119)
at org.robolectric.internal.bytecode.SandboxClassLoader.lambda$findClass[=13=](SandboxClassLoader.java:112)
at org.robolectric.util.PerfStatsCollector.measure(PerfStatsCollector.java:53)
at org.robolectric.internal.bytecode.SandboxClassLoader.findClass(SandboxClassLoader.java:111)
at java.lang.ClassLoader.loadClass(ClassLoader.java:419)
at java.lang.ClassLoader.loadClass(ClassLoader.java:352)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:264)
at dagger.hilt.android.internal.testing.TestApplicationComponentManager.<init>(TestApplicationComponentManager.java:58)
at dagger.hilt.android.testing.HiltTestApplication.attachBaseContext(HiltTestApplication.java:39)
at android.app.Application.$$robo$$android_app_Application$attach(Application.java:212)
at android.app.Application.attach(Application.java)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
... 11 more
Process finished with exit code 255
这是我的 app/build.gradle
具有纯 instrumentationTest (androidTest)
的同一文件仍然有效。
你错过了这个
kaptTest 'com.google.dagger:hilt-android-compiler:2.28-alpha'
来自您的依赖项..
来源:https://developer.android.com/training/dependency-injection/hilt-testing
dependencies {
// For Robolectric tests.
testImplementation 'com.google.dagger:hilt-android-testing:2.28-alpha'
// ...with Kotlin.
kaptTest 'com.google.dagger:hilt-android-compiler:2.28-alpha'
// ...with Java.
testAnnotationProcessor 'com.google.dagger:hilt-android-compiler:2.28-alpha'
// For instrumented tests.
androidTestImplementation 'com.google.dagger:hilt-android-testing:2.28-alpha'
// ...with Kotlin.
kaptAndroidTest 'com.google.dagger:hilt-android-compiler:2.28-alpha'
// ...with Java.
androidTestAnnotationProcessor 'com.google.dagger:hilt-android-compiler:2.28-alpha'
}
使匕首柄与 Robolectric 正常工作。请确保您具备以下条件:
- 从您的依赖项中获得
kaptTest 'com.google.dagger:hilt-android-compiler:...'
。 - 在你的测试中有
@HiltAndroidTest
class。 - 在您的测试 class 中有
@Config(application = HiltTestApplication::class)
或在 robolectric.properties 文件中指定要使用的应用程序:application=dagger.hilt.android.testing.HiltTestApplication