Skip to content

Commit

Permalink
add hilt testing dependencies
Browse files Browse the repository at this point in the history
Error: java.lang.IllegalStateException: Given component holder class androidx.activity.ComponentActivity does not implement interface dagger.hilt.internal.GeneratedComponent or interface dagger.hilt.internal.GeneratedComponentManager
  • Loading branch information
omer358 committed Aug 20, 2024
1 parent dfceb66 commit 091b045
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 4 deletions.
13 changes: 12 additions & 1 deletion app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ android {
versionCode = 1
versionName = "1.0"

testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
testInstrumentationRunner = "com.example.rememberme.CustomTestRunner"
vectorDrawables {
useSupportLibrary = true
}
Expand Down Expand Up @@ -105,6 +105,17 @@ dependencies {
implementation(libs.hilt.android)
implementation(libs.androidx.hilt.navigation.compose)
kapt(libs.hilt.compiler)

// For Robolectric tests.
testImplementation(libs.hilt.android.testing)
// ...with Kotlin.
kspTest(libs.hilt.android.compiler)

// For instrumented tests.

androidTestImplementation(libs.hilt.android.testing)
// ...with Kotlin.
kspAndroidTest(libs.hilt.android.compiler)
// Hilt Worker
implementation(libs.androidx.hilt.work)
// When using Kotlin.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.example.rememberme

import android.app.Application
import android.content.Context
import androidx.test.runner.AndroidJUnitRunner
import dagger.hilt.android.testing.HiltTestApplication

// A custom runner to set up the instrumented application class for tests.
class CustomTestRunner : AndroidJUnitRunner() {

override fun newApplication(cl: ClassLoader?, name: String?, context: Context?): Application {
return super.newApplication(cl, HiltTestApplication::class.java.name, context)
}
}
Original file line number Diff line number Diff line change
@@ -1,19 +1,23 @@
package com.example.rememberme
package com.example.rememberme.presentation.navgraph

import androidx.compose.ui.test.assertIsDisplayed
import androidx.compose.ui.test.junit4.createComposeRule
import androidx.compose.ui.test.onNodeWithTag
import androidx.compose.ui.test.onNodeWithText
import androidx.test.ext.junit.runners.AndroidJUnit4
import com.example.rememberme.presentation.navgraph.NavGraph
import com.example.rememberme.presentation.navgraph.Routes
import dagger.hilt.android.testing.HiltAndroidRule
import dagger.hilt.android.testing.HiltAndroidTest
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith

@RunWith(AndroidJUnit4::class)
@HiltAndroidTest
class NavGraphTest {

@get:Rule
var hiltRule = HiltAndroidRule(this)

@get:Rule
val composeTestRule = createComposeRule()

Expand Down
3 changes: 3 additions & 0 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ coreTestingVersion = "1.1.1"
datastorePreferences = "1.1.1"
firebaseBom = "33.1.2"
gradle = "8.0.2"
hiltAndroidTesting = "2.44"
hiltCompiler = "2.49"
hiltAndroidGradlePlugin = "2.49"
hiltNavigationCompose = "1.2.0"
Expand Down Expand Up @@ -56,6 +57,8 @@ core-testing = { module = "android.arch.core:core-testing", version.ref = "coreT
firebase-bom = { module = "com.google.firebase:firebase-bom", version.ref = "firebaseBom" }
gradle = { module = "com.android.tools.build:gradle", version.ref = "gradle" }
hilt-android = { module = "com.google.dagger:hilt-android", version.ref = "hiltCompiler" }
hilt-android-compiler = { module = "com.google.dagger:hilt-android-compiler", version.ref = "hiltAndroidTesting" }
hilt-android-testing = { module = "com.google.dagger:hilt-android-testing", version.ref = "hiltAndroidTesting" }
hilt-compiler = { module = "com.google.dagger:hilt-compiler", version.ref = "hiltCompiler" }
androidx-hilt-navigation-compose = { module = "androidx.hilt:hilt-navigation-compose", version.ref = "hiltNavigationCompose" }
hilt-android-gradle-plugin = { module = "com.google.dagger:hilt-android-gradle-plugin", version.ref = "hiltAndroidGradlePlugin" }
Expand Down

0 comments on commit 091b045

Please sign in to comment.