Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dagger Hilt not injecting on tests

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:

  • https://github.com/lucasnlm/antimine-android/pull/100
like image 961
Lucas Lima Avatar asked Jun 15 '20 16:06

Lucas Lima


Video Answer


1 Answers

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
  }

  ...
}
like image 96
Hiroyuki Tamura Avatar answered Oct 09 '22 07:10

Hiroyuki Tamura