DaggerMock icon indicating copy to clipboard operation
DaggerMock copied to clipboard

Mocked object not being used

Open hendrep opened this issue 7 years ago • 5 comments

I have tried implementing everything exactly as the sample project - not sure what I am missing, or maybe I am just not understanding how Mockito works...

I am injecting mock presenter into my test, but when test runs the activity is not using the mocked presenter.

If I debug the test and I inspect the presenter, and the activity's presenter I can see that they are not the same: Debug Watches: presenter (in test) = SealCheckContract$Presenter$MockitoMock$999498069 mActivityTestRule.getActivity().presenter = SealCheckPresenter

The activity calls a method on the presenter, presenter.isEarphonesConnected(), I need this to always return true. So in my test I have: when(presenter.isEarphonesConnected()).thenReturn(true);

If I do presenter.isEarphonesConnected() in the test it returns true correctly, but in the activity it still returns false (obviously because it is not using the mocked presenter)

I am using Dagger Android (so I used the EspressoDaggerMockRule as per the documentation)

Any ideas on what I could be doing wrong? How do I force the activity to use the mocked presenter?

Edit: Is it because of this? objects defined in subcomponent/dependent component cannot be replaces, DaggerMock works only for objects defined in application component If so, how would I then replace the presenter?

hendrep avatar Jan 06 '18 14:01 hendrep

Hi, unfortunately if you use Dagger Android you can replace only objects defined in the application component.

fabioCollini avatar Jan 08 '18 20:01 fabioCollini

Ok thanks. So does this mean that if I use DaggerMock and Dagger Android I am always going to have a big AppModules file? Or what is the correct way to deal with this?

hendrep avatar Jan 09 '18 06:01 hendrep

It depends on the kind of test you want to write and how you organize the code. Usually I try to keep the presenter/viewModel as simple as possible and move the logic to other objects (that can be defined in the application component). Then in the espresso test I use the real presenter/viewModel mocking the other objects. You don't need to use a single AppModule, you can split it in multiple modules. If you want to test the view in isolation with a mock presenter/viewModel you can't use DaggerMock with Dagger Android. But I am not sure how you can replace the presenter/viewModel even without DaggerMock.

fabioCollini avatar Jan 09 '18 18:01 fabioCollini

I having the same issue here. So does that mean daggermock doesnt work for injecting mocked presenters for activities?

i have already created a separate test module and test app component that has all the provides metthods inside one appModule in the base of the test app component yet it cant find and use that?

jonneymendoza avatar Jun 07 '18 17:06 jonneymendoza

If you are using Dagger Android you can't use DaggerMock to replace the presenter/viewModel in an espresso test. However it's not easy to replace it without DaggerMock eather. If you are not using Dagger Android it should work, if you are using Kotlin you should pay attention and use something to open the classes

fabioCollini avatar Jun 07 '18 19:06 fabioCollini