需要帮助为EmailSender编写测试

时间:2018-06-27 11:36:30

标签: unit-testing lambda java-8 mockito

我有一个发送邮件的类,如下所示:

public class AsyncEmailSender {
    /**
     * Logger.
     */
    private static final Logger LOG = LoggerFactory.getLogger(AsyncEmailSender.class);

    /**
     * Threadpool
     */
    private static final ExecutorService WORKERS = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors());

    /**
     * Java mail sender
     */
    private final JavaMailSender javaMailSender;

    /**
     * GSL Config  bean
     */
    private final GslConfig gslConfig;

    @Autowired
    public AsyncEmailSender(final JavaMailSender javaMailSender, final GslConfig gslConfig) {
        this.javaMailSender = javaMailSender;
        this.gslConfig = gslConfig;
    }

    /**
     * Sends the user creation email.
     *
     * @param addUserResponse adduserresponse.
     * @return AddUserResponse.
     */
    public AddUserResponse sendUserCreationEmailAsync(final AddUserResponse addUserResponse) {
        WORKERS.submit(() -> sendUserCreationEmail(addUserResponse));
        return addUserResponse;
    }

    /**
     * Package protected for unit testing.
     *
     * @param addUserResponse {@link AddUserResponse}.
     */
    void sendUserCreationEmail(final AddUserResponse addUserResponse) {
        SimpleMailMessage message = new SimpleMailMessage();
        message.setFrom(gslConfig.getValueAsString("datalake.email.username"));
        message.setTo(addUserResponse.getEmailAddress());
        message.setSubject(gslConfig.getValueAsString("datalake.email.createuser.subject"));
        message.setText(String.format(gslConfig.getValueAsString("datalake.email.createuser.content"),
                addUserResponse.getUsername() + "\n", addUserResponse.getPassword()));
        // Log as info as there won't be too many of these.
        if (LOG.isDebugEnabled()) {
            LOG.debug("Sending new user mail to: {}", addUserResponse.getEmailAddress());
        }
        javaMailSender.send(message);
    }

我需要测试sendUserCreationEmailAsync()方法,因为目前我无法测试。我已经为sendUserCreationEmail()编写了一个测试用例,如下所示:

@Test
    public void sendUserCreationEmail() {
        ArgumentCaptor<SimpleMailMessage> mailMessageArgumentCaptor = ArgumentCaptor.forClass(SimpleMailMessage.class);
        AddUserResponse addUserResponse = new AddUserResponse("username", "password", "email@email.com");
        when(gslConfig.getValueAsString("datalake.email.username")).thenReturn("admin@admin.com");
        when(gslConfig.getValueAsString("datalake.email.createuser.subject")).thenReturn("Email Subject");
        when(gslConfig.getValueAsString("datalake.email.createuser.content")).thenReturn("Your username: %s Your password: %s");
        when(logger.isDebugEnabled()).thenReturn(true);
        asyncEmailSender.sendUserCreationEmail(addUserResponse);
        verify(javaMailSender).send(mailMessageArgumentCaptor.capture());
        assertThat(mailMessageArgumentCaptor.getValue().getFrom(), equalTo("admin@admin.com"));
        assertThat(mailMessageArgumentCaptor.getValue().getTo().length, equalTo(1));
        assertThat(mailMessageArgumentCaptor.getValue().getTo()[0], equalTo("email@email.com"));
        assertThat(mailMessageArgumentCaptor.getValue().getSubject(), equalTo("Email Subject"));
        assertThat(mailMessageArgumentCaptor.getValue().getText(), equalTo("Your username: username\n Your password: password"));
    }

此测试正常。有人可以为我提供有关如何测试Async方法的帮助吗。

0 个答案:

没有答案