Create a gist now

Instantly share code, notes, and snippets.

Embed
TestUserServiceSpyV1
public class TestUserServiceWithSpies {
private static final Email EMAIL = new Email("user@company.com");
private static final User NO_FOUND_USER = new NoUser();
private static final User USER = null;
private final class StubUserDaoThatDoesntFoundUser implements UserDAO {
public User findUserBy(Email email) {
return NO_FOUND_USER;
}
public User createWith(Email email) {
// this isn't important for this example
return USER;
}
}
private final class SpyEmailService implements EmailService {
private boolean sentEmail = false;
public void sendEmailTo(User user) {
this.sentEmail = true;
}
public boolean wasSentEmail() {
return sentEmail;
}
}
@Test
public void //
should_send_an_email_when_a_user_was_created() throws Exception {
UserDAO stubUserDAO = new StubUserDaoThatDoesntFoundUser();
SpyEmailService spyEmailService = new SpyEmailService();
UserService userService = new UserService(stubUserDAO, spyEmailService);
userService.findOrCreateWith(EMAIL);
assertThat(spyEmailService.wasSentEmail(), is(true));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment