Skip to content

org.mockito.exceptions.base.MockitoException: Mockito cannot mock this class: class MainModule. #97

@noloman

Description

@noloman

Hi,

first of all congratulations and thanks for the library, because to be honest setting up Dagger is really cumbersome.

I'm trying DaggerMock for the first time to mock my dependencies in the instrumented tests, and I'm getting the errors here.

I'm having a fairly simple test just to make sure it works:

@RunWith(AndroidJUnit4::class)
class GoalEvaluationEntryFragmentTest {
    @get:Rule
    var activityRule = ActivityTestRule(GoalEvaluationActivity::class.java)
    @get:Rule
    val rule = espressoDaggerMockRule()

    @Before
    fun setup() {
        MockitoAnnotations.initMocks(this)
        EspressoTestUtil.disableProgressBarAnimations(activityRule)
        val fragment = GoalEvaluationEntryFragment()
        (activityRule.activity as FragmentActivity).supportFragmentManager.beginTransaction()
            .replace(R.id.mainContainer, fragment).commit()
    }

    @Test
    fun a() {

    }
}

and I'm using the espressoDaggerModule in the examples but adapted to my project:

fun espressoDaggerMockRule() =
    DaggerMock.rule<AppComponent>(
        MainModule(),
        ServicesModule(app),
        ModelsModule(app)
    ) {
        set { component -> app.component = component }
    }

val app: MDApplication get() = ApplicationProvider.getApplicationContext<Context>() as MDApplication

And the MainModule for example is:

@Module
@OpenForTesting
class MainModule {
    @Provides
    @Singleton
    fun provideUiHandler(): Handler = Handler(Looper.getMainLooper())

    @Provides
    @Singleton
    fun provideSnackBar(): SnackBar = SnackBar()
}

I tried to use both Kotlin AllOpen plugin and also DexOpener, but I'm always getting the same issue (the one in pastebin).
If I remove @get:Rule val rule = espressoDaggerMockRule() from the test, it all works (but I can't mock my dependencies of course).

Is there anything else I need to do that I've maybe missed?
Thanks!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions