5

Greenmail を使用して、ローカルホストで電子メール機能を単体テストしようとしています。問題は、SMTP サーバーをまだインストールしていないことです。インストールするのは面倒だと感じています。私の期待は、送信メールコードを同じに保つことができる無料のライブラリがあるはずですが、実際に電子メールをSMTPサーバーに送信するのではなく、ローカルマシンに送信して、それらを取得できるようにすることです(ユニットのために)テスト)。

Grails での開発時に Greenmail を使用したことがありますが、その経験は素晴らしいものです。しかし、Spring Framework に似たものは見つかりません。Greenmailのページには、JBoss にバンドルされたモック SMTP サーバーがあると書かれていますしかし、現在私の Web アプリケーションは Tomcat で実行されているため、JBoss を実行したくありません。

Tomcat 用の同様のサービスは既にありますか? または、送信された電子メールを取得できるローカルホストにテスト電子メールを送信するより良い方法はありますか?

更新

Ralph の方法を使用してみましたが、それでも同じ例外が発生します。

[SimpleAsyncTaskExecutor-1] 2012-03-13 10:19:39,475 - ERROR: com.test.my.service.emailing.impl.EmailServiceImpl - Mail server connection failed; nested exception is javax.mail.MessagingException: Could not connect to SMTP host: localhost, port: 3025;
  nested exception is:
    java.net.ConnectException: Connection refused: connect. Failed messages: javax.mail.MessagingException: Could not connect to SMTP host: localhost, port: 3025;
  nested exception is:
    java.net.ConnectException: Connection refused: connect
org.springframework.mail.MailSendException: Mail server connection failed; nested exception is javax.mail.MessagingException: Could not connect to SMTP host: localhost, port: 3025;
  nested exception is:
    java.net.ConnectException: Connection refused: connect. Failed messages: javax.mail.MessagingException: Could not connect to SMTP host: localhost, port: 3025;
  nested exception is:
    java.net.ConnectException: Connection refused: connect; message exception details (1) are:
Failed message 1:
javax.mail.MessagingException: Could not connect to SMTP host: localhost, port: 3025;
  nested exception is:
    java.net.ConnectException: Connection refused: connect
    at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1391)
    at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:412)
    at javax.mail.Service.connect(Service.java:288)
    at org.springframework.mail.javamail.JavaMailSenderImpl.doSend(JavaMailSenderImpl.java:389)
    at org.springframework.mail.javamail.JavaMailSenderImpl.send(JavaMailSenderImpl.java:340)
    at org.springframework.mail.javamail.JavaMailSenderImpl.send(JavaMailSenderImpl.java:336)
    at com.test.my.service.emailing.impl.EmailServiceImpl.test(EmailServiceImpl.java:388)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:309)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:183)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:150)
    at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:110)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
    at org.springframework.aop.interceptor.AsyncExecutionInterceptor$1.call(AsyncExecutionInterceptor.java:80)
    at java.util.concurrent.FutureTask$Sync.innerRun(Unknown Source)
    at java.util.concurrent.FutureTask.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)
Caused by: java.net.ConnectException: Connection refused: connect
    at java.net.PlainSocketImpl.socketConnect(Native Method)
    at java.net.PlainSocketImpl.doConnect(Unknown Source)
    at java.net.PlainSocketImpl.connectToAddress(Unknown Source)
    at java.net.PlainSocketImpl.connect(Unknown Source)
    at java.net.SocksSocketImpl.connect(Unknown Source)
    at java.net.Socket.connect(Unknown Source)
    at java.net.Socket.connect(Unknown Source)
    at com.sun.mail.util.SocketFetcher.createSocket(SocketFetcher.java:233)
    at com.sun.mail.util.SocketFetcher.getSocket(SocketFetcher.java:189)
    at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1359)
    ... 19 more
4

5 に答える 5

15

Spring を使用しているかどうかに関係なく、任意の Java プログラムでGreenmailを使用できます。Spring 固有のものは必要ありません。

ある種のインプロセス メール サーバーを実行します。

import com.icegreen.greenmail.util.GreenMail;
import com.icegreen.greenmail.util.GreenMailUtil;
import com.icegreen.greenmail.util.ServerSetupTest; ...

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("ApplicationContext-Greenmail.xml")
public class EmailServiceIntegrationTest {

    /** Class under test */
    @Resource
    private EmailService emailService;

    private GreenMail greenMail;

    @Before
    public void startMailServer() {
        greenMail = new GreenMail(ServerSetupTest.SMTP);
        greenMail.start();
    }

    @After
    public void stopMailServer() {
        greenMail.stop();
    }

    @Test
    public void testPledgeReminder()
                throws InterruptedException, MessagingException {

        String mailText = "Hallo World";
        String mailSubject = "Hallo";
        String mailTo = "test@excaple.com";

        /** when: sending a mail */
        emailService.send(mailSubject, mailTo, mailText);

        assertTrue(greenMail.waitForIncomingEmail(5000, 1));
        Message[] messages = greenMail.getReceivedMessages();
        assertEquals(1, messages.length);
        assertEquals(mailText, messages[0].getSubject());       
        String body = GreenMailUtil.getBody(messages[0]).replaceAll("=\r?\n", "");
        assertEquals(mailText, body);       
    }

}

重要: ポート 3025 を使用してください

<bean id="javaMailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
    <property name="javaMailProperties">
        <util:properties>
            <prop key="mail.debug">false</prop>
            <prop key="mail.transport.protocol">smtp</prop>
            <prop key="mail.smtp.port">3025</prop>
            <prop key="mail.smtp.auth">true</prop>
            <prop key="mail.smtp.user">test@mail.extern</prop>
            <prop key="mail.smtp.host">localhost</prop>
            <prop key="mail.smtp.from">test@mail.extern</prop>
        </util:properties>
    </property>
    <property name="username" value="test"/>
    <property name="password" value="xxx"/>
    <property name="defaultEncoding" value="utf8" />
</bean>

次に、この構成は Spring JavaMailSender を構成して、テストコードによって開始および終了された GreenMail-Server にメールを送信します。

于 2012-03-12T07:52:05.060 に答える
2

編集(2017年11月):

Dumbsterリンクが無効になっています、githubhttps://github.com/kirviq/dumbsterにフォークがあります

- - オリジナルメッセージ - -

Dumbsterを見てください-送信された実際の電子メールに対して主張できるので、すべての単体テストに使用しています。

http://quintanasoft.com/dumbster/

于 2012-03-12T08:14:23.680 に答える
0

誰かがかつてそのような統合を提案したようです: http://sourceforge.net/mailarchive/forum.php?thread_name=45DEB9E3.6090608%40consol.de&forum_name=greenmail-developers

現在も稼働しているかどうかはわかりません...

于 2012-03-12T08:01:13.553 に答える
0

springテストでこのようなことをするとうまくいきます:

ServerSetupTest.setPortOffset(<SOME_CUSTOM_PORT>);
testEmailServer = new GreenMail();
testEmailServer.start();

ServerSetupTestクラスの背後にある主なアイデアはServerSetup、テスト用にユーザー定義のポート オフセットを使用して を作成することです。詳細については、ドキュメントを参照してください。

于 2016-04-14T15:49:56.863 に答える