class UserRegistration {

    public void registerNewUser(String emailAddress) throws UserAlreadyRegisteredException, EmailFailedException {
        if (database.hasUser(emailAddress)) {
            throw new UserAlreadyRegisteredException();
        }

        if(!emailSender.sendRegistrationEmail(new RegistrationEmail(emailAddress))) {
            throw new EmailFailedException();
        }
        database.addUser(emailAddress);
    }

}

class UserRegistrationTest {

    private UserRegistration testee;
    private Database mockDatabase;
    private EmailSender mockEmailSender;

    @Test
    public void shouldAddNewUserToDatabase() throws UserAlreadyRegisteredException, IOException {

        UserRegistration testee = new UserRegistration(mockDatabase, mockEmailSender);

        String emailAddress = "foo@example.com";
        when(mockDatabase.hasUser(emailAddress)).thenReturn(false);

        when(mockEmailSender
                .sendRegistrationEmail(any(RegistrationEmail.class)))
                .thenReturn(true);

        testee.registerNewUser(emailAddress);
        Mockito.verify(mockDatabase).addUser(emailAddress);
    }

}