I understand that Mockito.verify() is used to ensure that a mock method is being called with required arguments. But I don't understand the intention of this. I often see tests similar to this:
public class UserDAO {
public long create(User user) {
//...
}
}
public class UserService {
private UserDAO userDAO;
public UserService(UserDAO userDAO) {
this.userDAO = userDAO;
}
public long createUser(User user) {
return userDAO.create(user);
}
}
public class UserServiceTest {
@Test
public void testCreateUser() {
UserDAO userDAO = mock(UserDAO.class);
when(userDAO.create(any(User.class))).thenReturn(anyLong());
UserService = new UserService(userDAO);
User user = new User("John Smith");
userService.createUser(user);
verify(userDAO).create(user);
}
}
Test verifies that create method of UserDAO is invoked when createUser method of UserService is invoked. It looks absurd. If I change the implementation of UserService in such way that it doesn't invoke method of UserDAO my test will fail even if the implementation is correct.
I admit that there might be cases when it's necessary to verify that method is invoked exact number of times but such cases are infrequent.
Most likely I don't understand the idea of verify and it's not a mockito-specific feature. Could you explain it in simple words and when it really makes sense to use it.
Aucun commentaire:
Enregistrer un commentaire