テスト環境では失敗し、同じ構成ではローカルに渡る統合テストがあります。 (もちろん、それを担当している開発者は休暇中です - 数値!)org.subethamail.wiserを使用して電子メール通知をテストしています。単体テストは、localhostと指定されたポートで待機するようにWiserを設定するだけで正常に動作しますが、統合テストは接続できません。別のステップが必要ですか?ここでsubethamailとの統合テストでConnectionRefused例外Wiser
は、関連するテストコード(単純化ビット)です:
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();
}
}
}
である。これは、次のログ:
予期しない例外をウェブフックの失敗メールを送信するときにスローされます。 com.sun.mail.util.MailConnectException:ホストに接続できませんでした。 port:localhost、3025;タイムアウト-1; ネストされた例外java.net.ConnectException:接続が拒否されました(Connection refused)電子メールprops: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に注入するための追加ステップが必要ですか?私が読んだドキュメントや例は、適切なホストとポートで聞く以外に何もしませんが、私が試したことはありません。