Skip to content

Instantly share code, notes, and snippets.

Avatar

Nyame Bismark Nyame123

View GitHub Profile
View custom_test_application.kt
@CustomTestApplication(BaseApplication::class)
interface HiltTestApplication
View binding_runtime.kt
@UninstallModules(AnalyticsModule::class)
@HiltAndroidTest
class SettingsActivityTest {
@BindValue @JvmField
val analyticsService: AnalyticsService = FakeAnalyticsService()
...
}
View only_one_test_provide.kt
@UninstallModules(AnalyticsModule::class)
@HiltAndroidTest
class SettingsActivityTest {
@Module
@InstallIn(SingletonComponent::class)
abstract class TestModule {
@Singleton
@Binds
View test_provide.kt
@Module
@TestInstallIn(
components = [SingletonComponent::class],
replaces = [AnalyticsModule::class]
)
abstract class FakeAnalyticsModule {
@Singleton
@Binds
abstract fun bindAnalyticsService(
View prod_module.kt
@Module
@InstallIn(SingletonComponent::class)
abstract class AnalyticsModule {
@Singleton
@Binds
abstract fun bindAnalyticsService(
analyticsServiceImpl: AnalyticsServiceImpl
): AnalyticsService
}
View doHiltTest.kt
@HiltAndroidTest
class SettingsActivityTest {
@get:Rule
var hiltRule = HiltAndroidRule(this)
@Inject
lateinit var analyticsAdapter: AnalyticsAdapter
@Before
View roboelectric_test.kt
@HiltAndroidTest
@Config(application = HiltTestApplication::class)
class SettingsActivityTest {
@get:Rule
var hiltRule = HiltAndroidRule(this)
// Robolectric tests here.
}
View hilt_test_runner.kt
// 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)
}
}
//gradle file add the testInstrumentationRunner
View hilt_test_ant.kt
@HiltAndroidTest
class SettingsActivityTest {
@get:Rule
var hiltRule = HiltAndroidRule(this)
// UI tests here.
}
View hilt_test_dep.kt
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.