简体   繁体   English

测试已发送的电子邮件-Dumbster和greenmail无法捕获已发送的JavaMail

[英]Testing for sent email - Dumbster and greenmail not catching sent JavaMail

I'm trying to integrate Dumbster to test our JavaMail based notifier for outgoing emails. 我正在尝试集成Dumbster来测试基于JavaMail的通知器是否有外发电子邮件。 The emails get sent but in my test Dumbster does not pick them up. 电子邮件已发送,但在我的测试中,Dumbster没有收到它们。 I'm not sure if I need additional configuration to make this work buton the dumbster homepage it says, it will listen automatically for mail sent through smtp on port 25. 我不确定是否需要其他配置才能完成此工作,但是在显示的dumbster主页上,它会自动侦听通过端口25上的smtp发送的邮件。

This is our java mail setup: 这是我们的Java邮件设置:

<bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
    <property name="host" value="smtp.somewhere.com" />
    <property name="port" value="25" />
    <property name="username" value="theUserName" />
    <property name="password" value="thePassword" />

    <property name="javaMailProperties">
        <props>
            <prop key="mail.smtp.auth">true</prop>
            <prop key="mail.smtp.starttls.enable">true</prop>
        </props>
    </property>
</bean>

Our Mailer class just injects the JavaMailer: 我们的Mailer类仅注入JavaMailer:

@Component
public class OurMailer {

    @Inject
    private MailSender mailSender;

    public void sendMail(String from, String to, String subject, String msg) {

        SimpleMailMessage message = new SimpleMailMessage();

        message.setFrom(from);
        message.setTo(to);
        message.setSubject(subject);
        message.setText(msg);
        mailSender.send(message);
    }

The test is quite straight forward as they demonstrate on their page: 正如他们在其页面上演示的那样,该测试非常简单:

@Inject
private OurMailer ourMailer;

@Test
public void ourMailer_should_send_mail() {
    SimpleSmtpServer server = SimpleSmtpServer.start();
    ourMailer.sendMail(FROM_EMAIL, TO_EMAIL, SUBJECT, MESSAGE);
    server.stop();
    Assert.assertTrue(server.getReceivedEmailSize() == 1);
}

As I said, the mail gets sent but the Assert fails. 如我所说,邮件已发送但断言失败。

Any ideas? 有任何想法吗?

BTW: I also tried Greenmail but with the same result: 顺便说一句:我也尝试了Greenmail,但结果相同:

maven slightly different: 专家略有不同:

    <dependency>
        <groupId>com.icegreen</groupId>
        <artifactId>greenmail</artifactId>
        <version>1.3.1b</version>
        <exclusions>
            <exclusion>
                <groupId>org.slf4j</groupId>
                <artifactId>slf4j-api</artifactId>
            </exclusion>
        </exclusions>
    </dependency>

The test 考试

    GreenMail greenMail = new GreenMail(); //uses test ports by default
    greenMail.start();
    // send mail
    Assert.assertEquals("subject", greenMail.getReceivedMessages()[0].getSubject());
    greenMail.stop();

The test fails because the config says "Send the mail to the SMTP server smtp.somewhere.com " but you want to send it to the SimpleSmtpServer which runs on localhost 测试失败,因为配置显示“将邮件发送到SMTP服务器smtp.somewhere.com ”,但是您要将其发送到在localhost上运行的SimpleSmtpServer

Use a System property for the host parameter of the mailSender bean and set it to localhost when you run a test. 使用System属性作为mailSender bean的host参数,并在运行测试时将其设置为localhost

That said, I suggest to split the test into two. 就是说,我建议将测试一分为二。 The first test should just make sure that the method is called with the correct parameters. 第一个测试应仅确保使用正确的参数调用该方法。 That way you will know that the code tries to send mails at the correct time; 这样,您将知道代码尝试在正确的时间发送邮件。 there is little point in testing SMTP authentication, your mail server, the mail infrastructure and the network in a unit test - all these are tested by their respective manufacturers. 在单元测试中测试SMTP身份验证,您的邮件服务器,邮件基础结构和网络毫无意义-所有这些均由其各自的制造商进行测试。

A second test should test just the method sendMail() method. 第二个测试应仅测试sendMail()方法。 Put that test into a test suite which can be run manually. 将该测试放入可以手动运行的测试套件中。 What you want to know here is whether you correctly set up and use the MailSender API. 您想了解的是您是否正确设置和使用MailSender API。 Unless you change the code in the sendMail() method, you don't have to run this test at all. 除非您更改sendMail()方法中的代码,否则根本不必运行此测试。

This will speed up your unit test and get rid of many unnecessary dependencies which can cause the test to fail even though your code works correctly. 这将加快单元测试的速度,并消除许多不必要的依赖关系,即使您的代码正常运行,这些依赖关系也可能导致测试失败。

Note: There seem to be newer versions of Dumbster (not available from the original source) that may have fixed the hang errors you encountered: 注意:似乎有更新的Dumbster版本(原始来源不可用)可能已解决您遇到的挂起错误:

sourceforge.net/p/dumbster/patches/9/ sourceforge.net/p/dumbster/patches/9/

https://github.com/rjo1970/dumbster https://github.com/rjo1970/dumbster

git-wip-us.apache.org/repos/asf?p=logging-log4j2.git;a=commit;h=b0f7be4b0666dca22205e6df5c7374677daa416e git-wip-us.apache.org/repos/asf?p=logging-log4j2.git;a=commit;h=b0f7be4b0666dca22205e6df5c7374677daa416e

And Greenmail appears to reside here these days: 这些天,Greenmail似乎住在这里:

https://github.com/greenmail-mail-test/greenmail https://github.com/greenmail-mail-test/greenmail

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM