Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mocked object not being used #66

Open
hendrep opened this issue Jan 6, 2018 · 5 comments
Open

Mocked object not being used #66

hendrep opened this issue Jan 6, 2018 · 5 comments

Comments

@hendrep
Copy link

hendrep commented Jan 6, 2018

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?

@fabioCollini
Copy link
Owner

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

@hendrep
Copy link
Author

hendrep commented Jan 9, 2018

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?

@fabioCollini
Copy link
Owner

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.

@jonneymendoza
Copy link

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?

@fabioCollini
Copy link
Owner

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants