I'm using Mosby and I would like to test my simple presenter.
public class DetailsPresenter extends MvpBasePresenter<DetailsView> {
public void showCountry(Country country) {
getView().setTitle(country.getName());
getView().setFlag(country.getFlagUrl());
}
}
I've tried to solve it by mocking Presenter:
public class DetailsPresenterTest {
private DetailsPresenter mockPresenter;
private DetailsView mockView;
@Before
public void setUp() throws Exception {
mockPresenter = mock(DetailsPresenter.class);
mockView = mock(DetailsView.class);
when(mockPresenter.isViewAttached()).thenReturn(true);
when(mockPresenter.getView()).thenReturn(mockView);
doCallRealMethod().when(mockPresenter).showCountry(any(Country.class));
}
@Test
public void shouldShowFlag() throws Exception {
mockPresenter.showCountry(any(Country.class));
verify(mockView, times(1)).setFlag(anyString());
}
@Test
public void shouldShowName() throws Exception {
mockPresenter.showCountry(any(Country.class));
verify(mockView, times(1)).setTitle(anyString());
}
}
But I've got the error
Wanted but not invoked:
detailsView.setFlag(<any string>);
-> at eu.szwiec.countries.details.DetailsPresenterTest.shouldShowFlag(DetailsPresenterTest.java:39)
Actually, there were zero interactions with this mock.
I've tried to use also real presenter without a success.
you have to use real Presenter and a real country object to invoke showCountry()
. Everything else doesnt make sense (not testing the real presenter but a mock presenter instance).
@Test
public void showFlagAndName(){
DetailsView mockView = mock(DetailsView.class);
DetailsPresenter presenter = new DetailsPresenter();
Country country = new Country("Italy", "italyFlag");
presenter.attachView(mockView);
presenter.showCountry(country);
verify(mockView, times(1)).showCountry("Italy");
verify(mockView, times(1)).setFlag("italyFlag");
}