无法使用JUnit模拟电子邮件服务器

时间:2013-06-20 18:16:30

标签: email junit mocking

我正在尝试模拟发送电子邮件(出于JUnit,v4.8.1,测试的目的)并决定使用Dumbster,我通过SO找到它。我正在使用1.6版本。我在JUnit测试中有这个...

    SimpleSmtpServer server = SimpleSmtpServer.start();
    boolean ret = m_emailSvc.sendEmail("me@me.com", 
                                       "you@you.com", 
                                       "localhost", 
                                       "Test", 
                                       "Test Body");
    Assert.assertTrue(ret);
    server.stop();

我以这种方式发送电子邮件......

public boolean sendEmail(final String toEmail,
                      final String fromEmail,
                      final String smtpHost,
                      final String subject,
                      final String body)
{

    boolean ret = true;
    // Get system properties
    Properties properties = System.getProperties();

    // Setup mail server
    properties.setProperty("mail.smtp.host", smtpHost);

    // Get the default Session object.
    Session session = Session.getDefaultInstance(properties);

    try{
       // Create a default MimeMessage object.
       MimeMessage message = new MimeMessage(session);

       // Set From: header field of the header.
       message.setFrom(new InternetAddress(fromEmail));

       // Set To: header field of the header.
       message.addRecipient(Message.RecipientType.TO,
                                new InternetAddress(toEmail));

       // Set Subject: header field
       message.setSubject(subject);

       // Now set the actual message
       message.setText(body);

       // Send message
       Transport.send(message);
    }catch (MessagingException mex) {
       ret = false;
       LOG.error(mex.getMessage(), mex);
    }   // try
    return ret;
}   // sendEmail

此操作失败,但下面有例外。有谁知道我做错了什么,或者有更简单的方法来模拟在JUnit测试中发送电子邮件?

java.net.BindException: Permission denied
    at java.net.PlainSocketImpl.socketBind(Native Method)
    at java.net.PlainSocketImpl.socketBind(PlainSocketImpl.java:521)
    at java.net.PlainSocketImpl.bind(PlainSocketImpl.java:414)
    at java.net.ServerSocket.bind(ServerSocket.java:326)
    at java.net.ServerSocket.<init>(ServerSocket.java:192)
    at java.net.ServerSocket.<init>(ServerSocket.java:104)
    at com.dumbster.smtp.SimpleSmtpServer.run(Unknown Source)
    at java.lang.Thread.run(Thread.java:680)
[ERROR]: org.mainco.subco.email.service.EmailServiceImpl - Could not connect to SMTP host: localhost, port: 25
javax.mail.MessagingException: Could not connect to SMTP host: localhost, port: 25;

嵌套异常是:         java.net.ConnectException:连接被拒绝         at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1934)         at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:638)         在javax.mail.Service.connect(Service.java:295)         在javax.mail.Service.connect(Service.java:176)         在javax.mail.Service.connect(Service.java:125)         在javax.mail.Transport.send0(Transport.java:194)         在javax.mail.Transport.send(Transport.java:124)         在org.mainco.subco.email.service.EmailServiceImpl.sendEmail(EmailServiceImpl.java:62)         在org.mainco.subco.email.service.EmailServiceTest.testSendEmail(EmailServiceTest.java:23)         at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)         at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)         at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)         在java.lang.reflect.Method.invoke(Method.java:597)         在org.junit.runners.model.FrameworkMethod $ 1.runReflectiveCall(FrameworkMethod.java:44)         在org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)         在org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)         在org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)         在org.junit.runners.BlockJUnit4ClassRunner.runNotIgnored(BlockJUnit4ClassRunner.java:79)         在org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:71)         在org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:49)         在org.junit.runners.ParentRunner $ 3.run(ParentRunner.java:193)         在org.junit.runners.ParentRunner $ 1.schedule(ParentRunner.java:52)         在org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)         在org.junit.runners.ParentRunner.access $ 000(ParentRunner.java:42)         在org.junit.runners.ParentRunner $ 2.evaluate(ParentRunner.java:184)         在org.junit.runners.ParentRunner.run(ParentRunner.java:236)         在org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)         在org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)         在org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)         在org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)         在org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)         在org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)     引起:java.net.ConnectException:连接被拒绝         at java.net.PlainSocketImpl.socketConnect(Native Method)         在java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:382)         在java.net.PlainSocketImpl.connectToAddress(PlainSocketImpl.java:241)         在java.net.PlainSocketImpl.connect(PlainSocketImpl.java:228)         在java.net.SocksSocketImpl.connect(SocksSocketImpl.java:384)         在java.net.Socket.connect(Socket.java:527)         在java.net.Socket.connect(Socket.java:476)         at com.sun.mail.util.SocketFetcher.createSocket(SocketFetcher.java:288)         at com.sun.mail.util.SocketFetcher.getSocket(SocketFetcher.java:231)         在com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1900)

2 个答案:

答案 0 :(得分:1)

我决定使用PowerMock(v.1.5.1)来模拟来自Transport类的静态调用。

@RunWith(PowerMockRunner.class)
public class EmailServiceTest
{

@Autowired
private EmailService m_emailSvc = new EmailServiceImpl();

@Test
@PrepareForTest( Transport.class )
public final void testSendEmail()
{
    suppress(methodsDeclaredIn(Transport.class));
    boolean ret = m_emailSvc.sendEmail("me@me.com",
                                       "you@you.com", 
                                       "localhost",
                                       "Test",
                                       "Test Body");
    Assert.assertTrue(ret);
}   // testSendEmail

答案 1 :(得分:0)

将调用包装到Transport怎么样是一个可模拟的对象?注入模拟并验证调用。

我的意思是这样的......

class MyTransport{
    public void send(MimeMessage message){
        Transport.send(message);
    }
}

然后将此类的实例注入上面的类中。在您的生产环境中,您拥有相同的代码。但是,在进行测试时,您可以传入MyTransport的模拟,从而在不需要服务器的情况下验证对send的调用。