Skip to content

Instantly share code, notes, and snippets.

@wkorando
Created April 21, 2020 18:08
Show Gist options
  • Save wkorando/01cc01b8f6eff878a5d175930480971b to your computer and use it in GitHub Desktop.
Save wkorando/01cc01b8f6eff878a5d175930480971b to your computer and use it in GitHub Desktop.
public class TestUserService {
@Test
public void testFindUserExisting() {
Map<Long, User> users = new HashMap<>();
users.put(100L, new User(100L, "John", "Doe"));
UserService service = new UserService(users, null);
User returnedUser = service.findUser(100L);
assertThat(returnedUser).isNotNull();
}
@Test
public void testFindUserNotExisting() {
Map<Long, User> users = new HashMap<>();
UserService service = new UserService(users, null);
ClientException e = assertThrows(ClientException.class, () -> service.findUser(100L));
assertThat(e.getMessage()).isEqualTo("User id: 100 not found!");
}
@Test
public void testCreateUserSuccessful() {
Map<Long, User> users = new HashMap<>();
UserService service = new UserService(users, new SequenceGenerator());
User newUser = service.createUser(new User("John", "Doe"));
assertThat(newUser.getId()).isGreaterThan(0L);
}
@Test
public void testCreateUserMissingFirstName() {
Map<Long, User> users = new HashMap<>();
UserService service = new UserService(users, new SequenceGenerator());
ClientException e = assertThrows(ClientException.class, () -> service.createUser(new User(null, "Doe")));
assertThat(e.getMessage()).isEqualTo("First name is a required field!");
}
@Test
public void testCreateUserMissingLastName() {
Map<Long, User> users = new HashMap<>();
UserService service = new UserService(users, new SequenceGenerator());
ClientException e = assertThrows(ClientException.class, () -> service.createUser(new User("John", "")));
assertThat(e.getMessage()).isEqualTo("Last name is a required field!");
}
@Test
public void testCreateUserMissingBothFields() {
Map<Long, User> users = new HashMap<>();
UserService service = new UserService(users, new SequenceGenerator());
ClientException e = assertThrows(ClientException.class, () -> service.createUser(new User("", "")));
assertThat(e.getMessage()).isEqualTo("First name is a required field!\nLast name is a required field!");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment