Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
public class TestUserServiceWithSpies {
private static final Email EMAIL = new Email("");
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) {
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;
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);
assertThat(spyEmailService.wasSentEmail(), is(true));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment