1

I'm working on app with Hilt and trying to make some instrumentation tests on it.

But my tests are crashing with:

java.lang.IllegalStateException: Hilt test, x.B, cannot use a @HiltAndroidApp application but found <...MyApplication>. To fix, configure the test to use HiltTestApplication or a custom Hilt test application generated with @CustomTestApplication.

Even tho I've made custom Runner : AndroidJUnitRunner that seems to be called properly since both logs are shown in logcat. But the super.newApplication call allways returns my main Application instead of HiltTestApplication

package x

// ...

class Runner : AndroidJUnitRunner() {

    override fun newApplication(
        cl: ClassLoader?,
        className: String?,
        context: Context?
    ): Application {
        Log.e("log", "HETTT")
        val app = super.newApplication(
            cl, HiltTestApplication::class.java.name, context
        )
        Log.e("log", "App made - ${app.javaClass.name}")

        return app
    }
}

and used it in gradle

android {
    defaultConfig {
        testInstrumentationRunner "x.Runner"
    }
}

Hilt gradle plugin, testing lib and compiler are set to '2.43.2' and JUnit runner is set to '1.4.0'

Can you think of something I might be doing wrong? Is there any way other plugins/libs can interfere with my configuration?

VizGhar
  • 3,036
  • 1
  • 25
  • 38

1 Answers1

0

OK, so I'm not sure what the problem exactly is, but I've managed to force my Runner to instantiate HiltTestApplication properly using this approach. Now everything seems to work just fine.

class Runner : AndroidJUnitRunner() {

    override fun newApplication(
        cl: ClassLoader,
        className: String,
        context: Context
    ): Application = Instrumentation.newApplication(HiltTestApplication::class.java, context)
}
VizGhar
  • 3,036
  • 1
  • 25
  • 38