Hilt Dependency Injection Instrumentation Test in Android

Viewed 3284

I am building a demo app using Jitpack Compose and decided to use Hilt for DI. Everything is working fine when I run the app, I get my injected dependencies with no issues and the app just runs successfully, however, when I try to run Instrumentation test, I keep getting the following error;

Execution failed for task ':app:kaptDebugAndroidTestKotlin'.
> A failure occurred while executing org.jetbrains.kotlin.gradle.internal.KaptExecution
   > java.lang.reflect.InvocationTargetException (no error message) 

No extra information to help me figure out the cause of it. I removed most of the DI related code to help me narrow down the issue, so it looks like this now;

MyApp.kt

@HiltAndroidApp
class MyApp : Application()

MainActivity.kt

@AndroidEntryPoint
class MainActivity : AppCompatActivity() {

    @Inject lateinit var newsRepository: LocalRepository

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
            TodayNewsApp(newsRepository)
        }
    }
}

LocalRepository.kt

class LocalRepository @Inject constructor() : NewsRepository {

    override fun getNews(callback: (Result<List<Article>>) -> Unit) {
        callback(Result.Success(articles))
    }
}

articles is just a list of dummy posts.

NewsRepository.kt

interface NewsRepository {

    fun getNews(callback: (Result<List<Article>>) -> Unit)
}

Regarding the test classes; I created a custom runner

MyCustomTestRunner.kt

class MyCustomTestRunner : AndroidJUnitRunner() {
    override fun newApplication(cl: ClassLoader?, name: String?, context: Context?): Application {
        return super.newApplication(cl, HiltTestApplication::class.java.name, context)
    }
}

and used it in app/build.gradle in defaultConfig

testInstrumentationRunner "app.abedelazizshe.todaynews.MyCustomTestRunner"

For testing classes, it does not work regardless of what the test does, for example;

TodayNewsUITest.kt

@HiltAndroidTest
class TodayNewsUITest {

    @get:Rule(order = 0)
    var hiltRule = HiltAndroidRule(this)

    @get:Rule(order = 1)
    val composeTestRule = createComposeRule()

    @Inject
    lateinit var newsRepository: LocalRepository

    @Before
    fun setup() {
        hiltRule.inject()
        composeTestRule.launchApp(newsRepository)
    }

    @Test
    fun app_launches() {
       findByText("TodayNews").assertIsDisplayed()
    }
}

and I am using

   androidTestImplementation "com.google.dagger:hilt-android-testing:2.28-alpha"
   kaptAndroidTest "com.google.dagger:hilt-android-compiler:2.28-alpha"

The moment i remove the following line from MainActivity.kt, the tests run successfully.

    @Inject lateinit var newsRepository: LocalRepository

I am using Android Studio Preview 4.2.

I'd appreciate your help

1 Answers

I faced this problem earlier, Please try to run your test using command , will give more detail about the Issue, and also in next step can try to debug your test instead of running.

./gradlew testdebug -stacktrace

and make sure in app build.gradle source and targetCompatibilty set to Java.Version_1_8

 compileOptions {
    targetCompatibility JavaVersion.VERSION_1_8
    sourceCompatibility JavaVersion.VERSION_1_8
}
Related