Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

java.lang.RuntimeException: Hilt classes generated from @HiltAndroidTest are missing

I am trying to run a simple Robolectric test with Hilt.

@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)
    }
}

but when I try to run the test using Android Studio, am getting

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

Here's the full stacktrace.

[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$2.lambda$evaluate$0(SandboxTestRunner.java:243)
    at org.robolectric.internal.bytecode.Sandbox.lambda$runOnMainThread$0(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$0(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

Here's my app/build.gradle

The same file with pure instrumentationTest (androidTest) works though.

like image 846
theapache64 Avatar asked Nov 29 '20 08:11

theapache64


2 Answers

You have missed this

kaptTest 'com.google.dagger:hilt-android-compiler:2.28-alpha'

from your dependencies..

source : 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'
}
like image 166
Chetan Gupta Avatar answered Oct 19 '22 01:10

Chetan Gupta


To make Dagger Hilt works normally with Robolectric. Please make sure you have the following conditionals:

  • Have kaptTest 'com.google.dagger:hilt-android-compiler:...' from your dependencies.
  • Have @HiltAndroidTest in your test class.
  • Have @Config(application = HiltTestApplication::class) in your test class or specify which application to use in the robolectric.properties file: application=dagger.hilt.android.testing.HiltTestApplication
like image 22
jenthone Avatar answered Oct 19 '22 00:10

jenthone