I'm trying to run a test using Dagger Hilt with Robolectric:
@RunWith(RobolectricTestRunner::class)
@UninstallModules(LevelModule::class, AppModule::class)
@Config(sdk = [16, 28], application = HiltTestApplication::class)
@LooperMode(LooperMode.Mode.PAUSED)
@HiltAndroidTest
class LevelFragmentTest {
@get:Rule
var rule = HiltAndroidRule(this)
@Test
fun testShowGameOverWhenTapAMine() {
launchActivity<GameActivity>().onActivity { activity ->
ShadowLooper.runUiThreadTasks()
...
}
}
The test fails on GameActivity.onCreate
because all fields of GameActivity with @Inject are null.
GameActivity is:
@AndroidEntryPoint
class GameActivity : AppCompatActivity()
The modules are:
@Module
@InstallIn(ActivityComponent::class)
open class LevelModule { ... }
@Module
@InstallIn(ActivityComponent::class)
class TestLevelModule {
@Module
@InstallIn(ApplicationComponent::class)
class AppModule() { ... }
@Module
@InstallIn(ApplicationComponent::class)
class TestAppModule() { ... }
It works when I run the app, but when I run the test, GameActivity
is not being injected. All field with @Inject
are null.
Does anyone have any idea of what it's wrong?
If useful, the whole code and test are here:
https://github.com/lucasnlm/antimine-android/pull/95
https://github.com/lucasnlm/antimine-android/pull/95/commits/fcc1b3782b8d456898529dd3ba2410ac5f2da6d5
EDIT
I have no ideia why, but the tests passed on this PR:
according to the testing guide,
you must replace the binding. Create a new module within the test class that defines the test binding:
@UninstallModules(AnalyticsModule::class)
@HiltAndroidTest
class SettingsActivityTest {
@Module
@InstallIn(ApplicationComponent::class)
abstract class TestModule {
@Singleton
@Binds
abstract fun bindAnalyticsService(
analyticsServiceImpl: AnalyticsServiceImpl
): AnalyticsService
}
...
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With