Imagine I have the following simple example interface:
public interface UserDB {
void addUser(User user);
void updateUser(User user);
User getUser(String id);
void deleteUser(String id);
}
I want to write tests with Mockito to test the simple CRUD operations. I want to verify that things like:
- Update/get/delete work only if the user was added before with 'add'
- They fail if the user was deleted before with 'delete'.
- Already created users cannot be created again
- etc.
My idea is, that I need something like this:
UserDB udb = mock(UserDB.class);
when(udb.getUser("1")).thenThrow(new UserNotFoundException());
when(udb.addUser(new User("1"))).when(udb.getUser("1").thenReturn(new User("1"));
However, things like the last line are not proper Mockito syntax. How can I check verify different results, for different preconditions or different orders of methods called?