我们的测试环境中的集成测试失败,但是使用相同的配置在本地传递。 (当然,负责它的开发人员正在度假 - 数字!)它正在使用org.subethamail.wiser来测试电子邮件通知。通过设置Wiser来监听localhost和指定端口,单元测试工作正常,但集成测试无法连接。还需要另一步吗?
这里是相关的测试代码(简化了一下):
private Wiser wiser;
@BeforeClass
private void init() {
wiser = new Wiser();
wiser.setPort(3025);
wiser.setHostname("localhost");
wiser.start();
}
@Test
public void errorNotificationTest() throws Exception {
WebHookRequest request = getWebHookRequest();
// modify request so that it will fail and set up headers - omitted because irrelevant
// Send webhook--the integration test fakes an HTTP request to a Spring controller method
ResultActions resultActions = webhookValidator.hook(request, headers, new HashMap<>());
webhookValidator.validateWebhook(resultActions, "Making sure we are able to trigger for dbid " + dbid);
// wait for webhook to be processed and the email to be sent--changing wait time does not help
Thread.sleep(120000);
Assert.assertEquals(wiser.getMessages().size(), 1, "failure email notification should be sent");
实际发送电子邮件的代码是
private void sendEmail(MimeMessage msg, Session session) throws MessagingException {
Transport transport = null;
try {
// Create a transport.
transport = session.getTransport();
transport.connect(host, smtpUsername, smtpPassword);
msg.saveChanges();
// Send the email.
transport.sendMessage(msg, msg.getAllRecipients());
} catch (Exception ex) {
LOGGER.sys().warn("Unexpected exception thrown when sending webhook failure email. {} {}", ex, propsForLogging() );
throw ex;
} finally {
// Close and terminate the connection.
if (transport != null) {
transport.close();
}
}
}
记录以下内容:
发送webhook失败电子邮件时抛出了意外的异常。 com.sun.mail.util.MailConnectException:无法连接到主机, port:localhost,3025;超时-1; 嵌套异常是:java.net.ConnectException:连接被拒绝(连接被拒绝)电子邮件道具:host = localhost, mail.smtp.starttls.enable = false,mail.smtp.port = 3025, mail.transport.protocol = smtp,mail.smtp.auth = false, mail.smtp.starttls.required =假
所以看起来属性设置正确。正如我所说,sendEmail的单元测试使用Wiser传递,但在这种情况下,代码在同一进程中直接调用。是否需要额外的步骤将Wiser注入Tomcat进行集成测试?我所阅读的文档和示例除了在适当的主机和端口上进行操作外,不做任何其他事情,但我尝试过的任何内容都无效。
答案 0 :(得分:0)
对此的简短回答,万一其他人偶然发现它:基本上,使用Wiser进行集成测试的整个概念都是错误的。集成测试是运行实际的进程,而不是模拟,并且没有办法将Wiser注入正在运行的Tomcat实例。我最终使用Wiser来增加功能测试,并且集成测试刚刚验证了它能够发送测试电子邮件而不会抛出异常。