I am just getting up to speed with Mockito and I don't find it particularly useful.
I have a View and a Presenter. The view is a dumb activity and the presenter contains all the business logic. I want to mock the View and test the way the Presenter works.
Here comes Mockito, I can successfully mock the View and these two unit tests work just fine:
@Test
public void testWhenUserNameIsEmptyShowErrorOnLoginClicked() throws Exception {
Mockito.when(loginView.getUserName()).thenReturn("");
Mockito.when(loginView.getPassword()).thenReturn("asdasd");
loginPresenter.setLoginView(loginView);
loginPresenter.onLoginClicked();
Mockito.verify(loginView).setEmailFieldErrorMessage();
}
@Test
public void testWhenPasswordIsEmptyShowErrorOnPasswordClicked() throws Exception {
Mockito.when(loginView.getUserName()).thenReturn("George");
Mockito.when(loginView.getPassword()).thenReturn("");
loginPresenter.setLoginView(loginView);
loginPresenter.onLoginClicked();
Mockito.verify(loginView).setPasswordFieldErrorMessage();
}
However, if I want to test the presenter's inner methods, this doesn't work:
@Test
public void testWhenUserNameAndPasswordAreEnteredShouldAttemptLogin() throws Exception {
LoginView loginView = Mockito.mock(LoginView.class);
Mockito.when(loginView.getUserName()).thenReturn("George");
Mockito.when(loginView.getPassword()).thenReturn("aaaaaa");
loginPresenter.setLoginView(loginView);
loginPresenter.onLoginClicked();
Mockito.verify(loginPresenter).attemptLogin(loginView.getUserName(), loginView.getPassword());
}
it throws a NotAMockException - it says the object should be a Mock. Why would I want to test the mock? It is one of the first rules in testing - you don't create a mock and then test it, you have an object that you want to test and if it needs some dependencies - you mock them.
Maybe I don't understand Mockito properly but it seems useless to me this way. What do I do?
Aucun commentaire:
Enregistrer un commentaire