diff --git a/app/build.gradle.kts b/app/build.gradle.kts index 35e9e77..19e4fc9 100644 --- a/app/build.gradle.kts +++ b/app/build.gradle.kts @@ -20,7 +20,7 @@ android { versionCode = 1 versionName = "1.0" - testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner" + testInstrumentationRunner = "com.example.rememberme.CustomTestRunner" vectorDrawables { useSupportLibrary = true } @@ -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. diff --git a/app/src/androidTest/java/com/example/rememberme/CustomTestRunner.kt b/app/src/androidTest/java/com/example/rememberme/CustomTestRunner.kt new file mode 100644 index 0000000..53fef35 --- /dev/null +++ b/app/src/androidTest/java/com/example/rememberme/CustomTestRunner.kt @@ -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) + } +} \ No newline at end of file diff --git a/app/src/androidTest/java/com/example/rememberme/presentation/navgraph/NavGraphTest.kt b/app/src/androidTest/java/com/example/rememberme/presentation/navgraph/NavGraphTest.kt index 6fba3b0..9e83e10 100644 --- a/app/src/androidTest/java/com/example/rememberme/presentation/navgraph/NavGraphTest.kt +++ b/app/src/androidTest/java/com/example/rememberme/presentation/navgraph/NavGraphTest.kt @@ -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() diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index 5d088ec..bbe0264 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -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" @@ -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" }