Spring Boot ã§ã¡ã¼ã«éä¿¡ãã Web ã¢ããªã±ã¼ã·ã§ã³ãä½ã ( ãã®ï¼ï¼ )( ã¡ã¼ã«éä¿¡ç»é¢ã®ä½æï¼ )
æ¦è¦
Spring Boot ã§ã¡ã¼ã«éä¿¡ãã Web ã¢ããªã±ã¼ã·ã§ã³ãä½ã ( ãã®ï¼ï¼ )( DataSource Bean ã®ç¬èªå®ç¾©ã¯ä¸è¦ï¼ä» ) ã®ç¶ãã§ãã
- ä»åã®æé ã§ç¢ºèªã§ããã®ã¯ä»¥ä¸ã®å
容ã§ãã
- ã¡ã¼ã«éä¿¡ç»é¢ã®ä½æ
- ä»åã¯ãã¹ãã¯ã©ã¹ãæ¸ãã¾ããé·ãã®ã§ï¼åã«åãã¾ãã
- Java ã®ã¡ã¼ã«éä¿¡ã® unit test ç¨ã©ã¤ãã©ãª WiserãGreenMail ã試ãã¦ã¿ã¾ãã
ã½ããã¦ã§ã¢ä¸è¦§
åèã«ãããµã¤ã
-
A SMTP server in Java for unit test
http://www.javatronic.fr/articles/2014/02/15/a_smtp_server_in_java_for_unit_test.html -
Working with a Java Mail Server for Testing
http://stackoverflow.com/questions/2782546/working-with-a-java-mail-server-for-testing?s=1|1.8801 SubEthaMailã®wiserã§ä»®æ³SMTPãç«ã¦ã¦javaã®ã¡ã¼ã«éä¿¡ãã¦ããããã¹ãããï¼
http://treeapps.hatenablog.com/entry/2015/01/10/012647GreenMail
http://www.icegreen.com/greenmail/this.getClass().getClassLoader().getResource(ââ¦â) and NullPointerException
http://stackoverflow.com/questions/3803326/this-getclass-getclassloader-getresource-and-nullpointerexception- ãã¹ããã¼ã¿ã® YAML ãã¡ã¤ã«ããã¹ãã¯ã©ã¹ã¨ã¯å¥ã®å ´æã«é
ç½®ããæã«
getClass().getResourceAsStream(...)
ã§ã©ãæå®ããã°ããã®ãåãããªãã£ãæã«åèã«ãã¾ããã
- ãã¹ããã¼ã¿ã® YAML ãã¡ã¤ã«ããã¹ãã¯ã©ã¹ã¨ã¯å¥ã®å ´æã«é
ç½®ããæã«
æé
unit test ç¨ã®ã¡ã¼ã«ãµã¼ãã®ã©ã¤ãã©ãªã¨ãã¦ä½ã使ç¨ãããï¼
- ããããããã¾ãã ( GreenMailã®ãRelated projects to JavaMail and testing/development:ãã¨æ¸ãããã¨ããã«ä¸»ãªã©ã¤ãã©ãªã®ä¸è¦§ãæ¸ããã¦ãã¾ã ) ã使ç¨ä¾ã®è¨äºãå¤ãã¨æãããã®ã¯ Wiser 㨠GreenMail ã§ããã
- Wiser ã¯ã¡ã¼ã«ã®éä¿¡ã¨éä¿¡ããã¡ã¼ã«ã®å 容ã®ç¢ºèªã®æ©è½ã®ã¿ã§ãããå®å®ãã¦ãã¦è»½å¿«ãããªå°è±¡ãããã¾ãã
- GreenMail 㯠SMTP 以å¤ã« POP3ãIMAP ããµãã¼ãããã¦ãããæ©è½ãè±å¯ã§ãã¼ã ãã¼ã¸ã綺éºã«ã¾ã¨ã¾ã£ã¦ããå°è±¡ã§ãã
- ã©ã¡ãã試ãã¦ã¿ããã¨æãã¾ãã
ãã¹ãä½æ対象ã®ã¯ã©ã¹
ä»åã¯ä»¥ä¸ã®ã¯ã©ã¹ã®ãã¹ãã¯ã©ã¹ãä½æãã¾ãã
- util/VelocityUtils
- helper/mail/MAIL001MailHelper
- service/EmailService
次åã以ä¸ã®ã¯ã©ã¹ã®ãã¹ãã¯ã©ã¹ãä½æãã¾ãã
- web/mailsend/MailsendService
- web/mailsend/MailsendController
ãã©ã³ãã®ä½æ
- IntelliJ IDEA 㧠1.0.x-maketest-mailsend ãã©ã³ããä½æãã¾ãã
JUnit ã®è¨å®
JUnit ã®è¨å®ãå¿ãã¦ããã®ã§è¨å®ãã¾ãã
ã¡ã¤ã³ã¡ãã¥ã¼ãããRunã-ãEdit Configurations...ããé¸æãã¾ãã
ãRun/Debug Configuraitonsããã¤ã¢ãã°ã表示ããã¾ããå·¦å´ã®ããªã¼ã§ãDefaultsã-ãJUnitããé¸æããå¾ãå³å´ã®ç»é¢ã®ãVM optionsãã®æ«å°¾ã«
-Dspring.profiles.active=unittest -Dfile.encoding=UTF-8
ã追å ãã¾ãã追å å¾ãOKããã¿ã³ãã¯ãªãã¯ãã¦ãã¤ã¢ãã°ãéãã¾ãã
â»ãã¨ã§ Spring Boot ã§ã¡ã¼ã«éä¿¡ãã Web ã¢ããªã±ã¼ã·ã§ã³ãä½ã ( ãã®ï¼ )( Project ã®ä½æ ) ã«ãã®æé ã追å ãã¦ããã¾ãã
util/VelocityUtils ã®ãã¹ãã¯ã©ã¹ã®ä½æ
ãã¹ãç¨ã® Velocity ãã³ãã¬ã¼ããã¡ã¤ã«ãä½æãã¾ããsrc/test/resources ã®ä¸ã« ksbysample/webapp/email/util ãã£ã¬ã¯ããªãä½æãã¾ãã
src/test/resources/ksbysample/webapp/email/util ã®ä¸ã« VelocityUtilsTest.vm ãæ°è¦ä½æãã¾ããä½æå¾ããªã³ã¯å ã®å 容 ã«å¤æ´ãã¾ãã
src/main/java/ksbysample/webapp/email/util ã®ä¸ã® VelocityUtils.java ãéããå¾ãã¯ã©ã¹åã«ã«ã¼ã½ã«ã移åã㦠Alt+Enter ãæ¼ãã¾ããã³ã³ããã¹ãã¡ãã¥ã¼ã表示ãããããCreate Testããé¸æãã¾ãã
ãCreate Testããã¤ã¢ãã°ã表示ããã¾ããç»é¢ä¸é¨ã®ãTesting libraryãã§ãJUnit4ããé¸æããç»é¢ä¸é¨ã®ã¡ã½ããä¸è¦§ãããã¹ã対象ã®ã¡ã½ããããã§ãã¯ããå¾ãOKããã¿ã³ãã¯ãªãã¯ãã¾ãã
src/test/java/ksbysample/webapp/email/util ã®ä¸ã« VelocityUtilsTest.java ãçæããã¾ãããªã³ã¯å ã®å 容 ã«å¤æ´ãã¾ãã
ãã¹ããå®è¡ãã¾ããVelocityUtilsTest.java ã® testMerge ã¡ã½ããã«ã«ã¼ã½ã«ã移åããå¾ãã³ã³ããã¹ãã¡ãã¥ã¼ã表示ãã¦ãRun 'testMerge()' with Coverageãã¡ãã¥ã¼ãé¸æãã¾ãã
ãã¹ããå®è¡ããã¦ãAll Test Passedãã®æåã表示ããã¾ãã
helper/mail/MAIL001MailHelper ã®ãã¹ãã¯ã©ã¹ã®ä½æ
ãã¹ãã§ä½¿ç¨ãã MailsendForm ã¯ã©ã¹ã®ãã¹ããã¼ã¿ãä½æãã¾ããsrc/test/resources/ksbysample/webapp/email ã®ä¸ã« web/mailsend ãã£ã¬ã¯ããªãä½æãã¾ãã
src/test/resources/ksbysample/webapp/email/web/mailsend ã®ä¸ã« mailsendForm_simple.yml, mailsendForm_minimum.yml ãæ°è¦ä½æãã¾ããä½æå¾ããªã³ã¯å ã®å 容 ã«å¤æ´ãã¾ãã
src/main/java/ksbysample/webapp/email/helper/mail ã® MAIL001MailHelper.java ãã MAIL001MailHelperTest.java ãçæãã¾ãã
src/test/java/ksbysample/webapp/email/helper/mail ã®ä¸ã® MAIL001MailHelperTest.java ã ãªã³ã¯å ã®å 容 ã«å¤æ´ãã¾ãã
ãã¹ããå®è¡ãã¾ããMAIL001MailHelperTest.java ã®ã¯ã©ã¹åã«ã«ã¼ã½ã«ã移åããå¾ãã³ã³ããã¹ãã¡ãã¥ã¼ã表示ãã¦ãRun 'MAIL001MailHelperTest' with Coverageãã¡ãã¥ã¼ãé¸æãã¾ãã
ãã¹ããå®è¡ããã¾ããããMailsendFormã®å¿ é é ç®ã®ã¿å¤ãã»ããããã¦ããå ´åãã®ãã¹ãã失æãã¾ããã
åå 㯠MAIL001MailHelper ã¯ã©ã¹ã® generateTextUsingVelocity ã¡ã½ãã㧠getItem() ã§åå¾ãããªã¹ãã Null ãã§ãã¯ãããã« Stream API ã§å¦çãã¦ãããããNullPointerException ãçºçãã¦ããããã§ããã
src/main/java/ksbysample/webapp/email/helper/mail ã®ä¸ã® MAIL001MailHelper.java ã ãªã³ã¯å ã®å 容 ã«å¤æ´ãã¾ãã
å度 MAIL001MailHelperTest.java ã®ã¯ã©ã¹åãããRun 'MAIL001MailHelperTest' with Coverageãã¡ãã¥ã¼ãé¸æãã¾ããä»åº¦ã¯ãã¹ããæ£å¸¸ã«çµäºãã¦ãAll Test Passedãã®æåã表示ããã¾ããã
Wiser ç¨ ExternalResource ç¶æ¿ã¯ã©ã¹ãservice/EmailService ã®ãã¹ãã¯ã©ã¹ã®ä½æ
build.gradle ã ãªã³ã¯å ã®å 容 ã«å¤æ´ãã¾ãã
Gradle projects View ã®å·¦ä¸ã«ãããRefresh all Gradle projectsãã¢ã¤ã³ã³ãã¯ãªãã¯ãã¦ãå¤æ´ãã build.gradle ã®å 容ãåæ ãã¾ãã
src/test/java/ksbysample/webapp/email ã®ä¸ã« test ããã±ã¼ã¸ãä½æãã¾ãã
src/test/java/ksbysample/webapp/email/test ã®ä¸ã« MailServerWiserResource.java ãæ°è¦ä½æãã¾ããä½æå¾ããªã³ã¯å ã®å 容 ã«å¤æ´ãã¾ãã
src/main/java/ksbysample/webapp/email/service ã® EmailService.java ãã EmailServiceTest.java ãçæãã¾ãã
src/test/java/ksbysample/webapp/email/service ã®ä¸ã® EmailServiceTest.java ã ãªã³ã¯å ã®ãã®ï¼ã®å 容 ã«å¤æ´ãã¾ãã
ãã¹ããå®è¡ãã¾ãããããã®æç¹ã§ smtp4dev ãèµ·åãã¦ããå ´åã«ã¯çµäºãã¦ä¸ãããèµ·åãã¦ãã㨠ãã¼ãçªå· 25 ã使ç¨ããã¦ãã¾ããããWiser ãèµ·åãã¾ããã
EmailServiceTest.java å ã®ã¯ã©ã¹åãWiserãå©ç¨ãã¦ã¡ã¼ã«éä¿¡ããå ´åãã«ã«ã¼ã½ã«ã移åããå¾ãã³ã³ããã¹ãã¡ãã¥ã¼ã表示ãã¦ãRun 'MAIL001MailHelperTest' with Coverageãã¡ãã¥ã¼ãé¸æãã¾ãã
ãã¹ããå®è¡ããã¦ãAll Test Passedãã®æåã表示ããã¾ãã
GreenMail ç¨ service/EmailService ã®ãã¹ãã¯ã©ã¹ã®ä½æ
GreenMail 㯠Rule ( ExternalResource ç¶æ¿ã¯ã©ã¹ ) ãç¨æããã¦ãã¾ãããã®ã¾ã¾å©ç¨ãã¦ã¿ã¾ãã
src/test/java/ksbysample/webapp/email/service ã®ä¸ã® EmailServiceTest.java ã ãªã³ã¯å ã®ãã®ï¼ã®å 容 ã«å¤æ´ãã¾ãã
ãã¹ããå®è¡ãã¾ããEmailServiceTest.java å ã®ã¯ã©ã¹åãGreenMailãå©ç¨ãã¦ã¡ã¼ã«éä¿¡ããå ´åãã«ã«ã¼ã½ã«ã移åããå¾ãã³ã³ããã¹ãã¡ãã¥ã¼ã表示ãã¦ãRun 'MAIL001MailHelperTest' with Coverageãã¡ãã¥ã¼ãé¸æãã¾ãã
ãã¹ããå®è¡ããã¦ãAll Test Passedãã®æåã表示ããã¾ãã
Wiser 㨠GreenMail ã®ã©ã¡ãã使ç¨ãããï¼
- ï¼éã¡ã¼ã«ãéä¿¡ããã ããªãã°ãã©ã¡ãã使ãåæãæ§è½ã¯å¤ããã¾ããã§ããã
- 大éã®ã¡ã¼ã«éä¿¡ããæ·»ä»ãã¡ã¤ã«ä»ãã¡ã¼ã«ã®éä¿¡ã§ã¯å¤ããã®ããããã¾ããããç¾æç¹ã§ã¯æ¤è¨¼ãã¦ããªãã®ã§ä½ã¨ãè¨ãã¾ããã
- POP3 ã IMAP ã®è©¦é¨ããããã¨ã¯ã¾ããªãã®ã§ãWiser ã§ãå ¨ç¶åé¡ãããã¾ããã
- GreenMail ã¯ãããããã©ã¤ãã©ãªã§ Rule ç¨ã®ã¯ã©ã¹ãç¨æããã¦ãããå¥éç¨æããå¿ è¦ããªãç¹ã¯ä¾¿å©ã§ãã
- stackoverflow ã§ãGreenMail ã§åé¡ãçºçããã®ã§ SubEthaSmtp/Wiser ã¸åãæ¿ããã¨ããåçãæ¸ãã¦ãã人ãããã®ã§ããã®ç¹ã¯ã¡ãã£ã¨æ°ã«ãªãã¾ãã
- jcenter ãããããã®ã©ã¤ãã©ãªã®ãã¼ã ãã¼ã¸ãè¦ã¦ã¿ãã¨ãWiser ã¯ï¼å¹´åãããã§æ´æ°ãæ¢ã¾ã£ã¦ãã¾ãããGreenMail 㯠2015/4/27 ã«ãã¼ã¸ã§ã³ 1.4.1 ããªãªã¼ã¹ããã¦ãã¾ããGreenMail ã¯å°ç¨ãã¼ã ãã¼ã¸ã綺éºã«ã¾ã¨ã¾ã£ã¦ããå°è±¡ãããã¾ãã
- 以ä¸ã®æ¹éã§é²ãããã¨ã«ãã¾ãã
- æ§è½ã®éãããããããªã Wiser ããªã¨èãã¦ããã®ã§ãããç¾æç¹ã§ã¯å·®ç°ã¯è¦ããããæ©è½ã®è±å¯ãããã¼ã ãã¼ã¸ã®ã¾ã¨ã¾ãå ·åãã GreenMail ã使ç¨ãããã¨æãã¾ãã
- ãã ã Rule ( ExternalResource ç¶æ¿ã¯ã©ã¹ ) 㯠GreenMail ã®ãã®ããã®ã¾ã¾ä½¿ç¨ãããå¥éä½æãã¾ããåé¡ãçºçãããããªãã° ExternalResource ç¶æ¿ã¯ã©ã¹ãå¤æ´ããã ã㧠Wiser çã®å¥ã®ã©ã¤ãã©ãªã«åãæ¿ãããã¨ãã§ããããã«ãã¦ããã¾ãã
ã¡ã¼ã«ãµã¼ãç¨ ExternalResource ç¶æ¿ã¯ã©ã¹ ( GreenMail 使ç¨ç ) ã®ä½æ
src/test/java/ksbysample/webapp/email/test ã®ä¸ã« MailServerResource.java ãæ°è¦ä½æãã¾ããä½æå¾ããªã³ã¯å ã®å 容 ã«å¤æ´ãã¾ãã
src/test/java/ksbysample/webapp/email/service ã®ä¸ã® EmailServiceTest.java ã ãªã³ã¯å ã®ãã®ï¼ã®å 容 ã«å¤æ´ãã¾ãã
- Wiser 㨠GreenMail ã®ãã¹ãã¯ã©ã¹ã¯ãã®ã¾ã¾æ®ãã¦ããã¾ãã
- MailServerResource ã使ç¨ãããã¹ãã¯ã©ã¹ã追å ãã¾ãã
- ãã¹ãã¯ã©ã¹å
ã®å
¨ã¦ã®ãã¹ãããã¯ã©ã¹ããã¹ããããã¨æã£ãã¨ããããã¹ãåºæ¥ãªã ( ãRun ... with Coverageãã®ã¡ãã¥ã¼ã表示ãããªã ) ãã¨ã«æ°ã¥ãã¾ãããEmailServiceTest ã¯ã©ã¹ã«
@RunWith(Enclosed.class)
ã追å ãã¦åºæ¥ãããã«ãã¾ãã
ä»åã¯ãã¹ãã¯ã©ã¹å ã®å ¨ã¦ã®ãã¹ããå®è¡ãã¦ã¿ã¾ããEmailServiceTest.java ã®ã¯ã©ã¹åã«ã«ã¼ã½ã«ã移åããå¾ãã³ã³ããã¹ãã¡ãã¥ã¼ã表示ãã¦ãRun 'EmailServiceTest' with Coverageãã¡ãã¥ã¼ãé¸æãã¾ãã
å ¨ã¦ã®ãã¹ããå®è¡ããã¦ãAll Test Passedãã®æåã表示ããã¾ãã
次åã¯ããã
ãã¹ãã¯ã©ã¹ã®ä½æã®ç¶ãã§ããweb/mailsend/MailsendService ã®ãã¹ãã¯ã©ã¹ãä½æãã¦ããã®ã§ãããæéãããã£ã¦ããã®ã§éä¸ã§åºåããã¨ã«ãã¾ãããcommit çã¯æ¬¡åå®æ½ãã¾ãã
ã½ã¼ã¹ã³ã¼ã
VelocityUtilsTest.vm
ãã¹ã: $testdata
VelocityUtilsTest.java
package ksbysample.webapp.email.util; import ksbysample.webapp.email.Application; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.SpringApplicationConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import org.springframework.test.context.web.WebAppConfiguration; import java.util.HashMap; import java.util.Map; import static org.hamcrest.Matchers.is; import static org.junit.Assert.assertThat; @RunWith(SpringJUnit4ClassRunner.class) @SpringApplicationConfiguration(classes = Application.class) @WebAppConfiguration public class VelocityUtilsTest { private final String templateLocation = "ksbysample/webapp/email/util/VelocityUtilsTest.vm"; @Autowired private VelocityUtils velocityUtils; @Test public void testMerge() throws Exception { Map<String, Object> model = new HashMap<>(); model.put("testdata", "VelocityUtils ã®ãã¹ãã§ãã"); String testString = velocityUtils.merge(templateLocation, model); assertThat(testString, is("ãã¹ã: VelocityUtils ã®ãã¹ãã§ãã")); } }
mailsendForm_simple.yml, mailsendForm_minimum.yml
â mailsendForm_simple.yml
!!ksbysample.webapp.email.web.mailsend.MailsendForm fromAddr: [email protected] toAddr: [email protected] subject: ãã¹ã name: ç°ä¸ã太é sex: 1 type: 1 item: - 101 - 102 - 103 naiyo: ããã¯ãã¹ãã§ãã
â mailsendForm_minimum.yml
!!ksbysample.webapp.email.web.mailsend.MailsendForm fromAddr: [email protected] toAddr: [email protected] subject: ãã¹ã name: sex: type: item: naiyo:
MAIL001MailHelperTest.java
package ksbysample.webapp.email.helper.mail; import ksbysample.webapp.email.Application; import ksbysample.webapp.email.web.mailsend.MailsendForm; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.SpringApplicationConfiguration; import org.springframework.mail.SimpleMailMessage; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import org.springframework.test.context.web.WebAppConfiguration; import org.yaml.snakeyaml.Yaml; import static org.hamcrest.Matchers.is; import static org.junit.Assert.assertThat; @RunWith(SpringJUnit4ClassRunner.class) @SpringApplicationConfiguration(classes = Application.class) @WebAppConfiguration public class MAIL001MailHelperTest { private final MailsendForm mailsendFormSimple = (MailsendForm) new Yaml().load(getClass().getResourceAsStream("/ksbysample/webapp/email/web/mailsend/mailsendForm_simple.yml")); private final MailsendForm mailsendFormMinimum = (MailsendForm) new Yaml().load(getClass().getResourceAsStream("/ksbysample/webapp/email/web/mailsend/mailsendForm_minimum.yml")); @Autowired private MAIL001MailHelper mail001MailHelper; @Test public void MailsendFormã®å ¨ã¦ã«å¤ãã»ããããã¦ããå ´å() throws Exception { SimpleMailMessage message = mail001MailHelper.createMessage(mailsendFormSimple); assertThat(message.getFrom(), is(mailsendFormSimple.getFromAddr())); } @Test public void MailsendFormã®å¿ é é ç®ã®ã¿å¤ãã»ããããã¦ããå ´å() throws Exception { SimpleMailMessage message = mail001MailHelper.createMessage(mailsendFormMinimum); assertThat(message.getFrom(), is(mailsendFormMinimum.getFromAddr())); } }
MAIL001MailHelper.java
private String generateTextUsingVelocity(MailsendForm mailsendForm) { Constant constant = Constant.getInstance(); Map<String, Object> model = new HashMap<>(); model.put("name", mailsendForm.getName()); model.put("sex", constant.SEX_MAP.get(mailsendForm.getSex())); model.put("type", constant.TYPE_MAP.get(mailsendForm.getType())); if (mailsendForm.getItem() != null) { String itemList = mailsendForm.getItem().stream() .map(constant.ITEM_MAP::get) .collect(Collectors.joining(", ")); model.put("item", itemList); } model.put("naiyo", mailsendForm.getNaiyo()); return velocityUtils.merge(this.templateLocation, model); }
- getItem() ã null ããã§ãã¯ãã
if (mailsendForm.getItem() != null) { ... }
ã追å ãã¾ãã
build.gradle
dependencies { def jdbcDriver = "org.postgresql:postgresql:9.4-1201-jdbc41" // spring-boot-gradle-plugin ã«ãããã¼ã¸ã§ã³çªå·ãèªåã§è¨å®ããããã® // Appendix E. Dependency versions ( http://docs.spring.io/spring-boot/docs/current/reference/html/appendix-dependency-versions.html ) åç § compile("org.springframework.boot:spring-boot-starter-web") compile("org.springframework.boot:spring-boot-starter-thymeleaf") compile("org.springframework.boot:spring-boot-starter-data-jpa") compile("org.springframework.boot:spring-boot-starter-velocity") compile("org.springframework.boot:spring-boot-starter-mail") compile("org.codehaus.janino:janino") testCompile("org.springframework.boot:spring-boot-starter-test") testCompile("org.yaml:snakeyaml") // spring-boot-gradle-plugin ã«ãããã¼ã¸ã§ã³çªå·ãèªåã§è¨å®ãããªããã® compile("${jdbcDriver}") compile("org.seasar.doma:doma:2.2.0") compile("org.bgee.log4jdbc-log4j2:log4jdbc-log4j2-jdbc4.1:1.16") compile("org.apache.commons:commons-lang3:3.4") compile("org.projectlombok:lombok:1.16.2") testCompile("org.dbunit:dbunit:2.5.0") testCompile("org.subethamail:subethasmtp:3.1.7") testCompile("com.icegreen:greenmail:1.4.1") // for Doma-Gen domaGenRuntime("org.seasar.doma:doma-gen:2.2.0") domaGenRuntime("${jdbcDriver}") }
testCompile("org.subethamail:subethasmtp:3.1.7")
ã追å ãã¾ããtestCompile("com.icegreen:greenmail:1.4.1")
ã追å ãã¾ãã
MailServerWiserResource.java
package ksbysample.webapp.email.test; import org.junit.rules.ExternalResource; import org.springframework.stereotype.Component; import org.subethamail.wiser.Wiser; @Component public class MailServerWiserResource extends ExternalResource { private Wiser wiser = new Wiser(); @Override protected void before() { this.wiser.setHostname("localhost"); this.wiser.setPort(25); this.wiser.start(); } @Override protected void after() { this.wiser.stop(); } public Wiser getWiser() { return this.wiser; } }
EmailServiceTest.java
â ãã®ï¼
package ksbysample.webapp.email.service; import ksbysample.webapp.email.Application; import ksbysample.webapp.email.test.MailServerWiserResource; import org.junit.Rule; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.SpringApplicationConfiguration; import org.springframework.mail.SimpleMailMessage; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import org.springframework.test.context.web.WebAppConfiguration; import org.subethamail.wiser.WiserMessage; import javax.mail.internet.InternetAddress; import javax.mail.internet.MimeMessage; import java.util.List; import static org.hamcrest.Matchers.is; import static org.junit.Assert.*; public class EmailServiceTest { @RunWith(SpringJUnit4ClassRunner.class) @SpringApplicationConfiguration(classes = Application.class) @WebAppConfiguration public static class Wiserãå©ç¨ãã¦ã¡ã¼ã«éä¿¡ããå ´å { @Rule @Autowired public MailServerWiserResource wiser; @Autowired private EmailService emailService; @Test public void testSendSimpleMail() throws Exception { SimpleMailMessage message = new SimpleMailMessage(); message.setFrom("[email protected]"); message.setTo("[email protected]"); message.setSubject("ãã¹ã"); message.setText("ããã¯ãã¹ãã§ã"); emailService.sendSimpleMail(message); List<WiserMessage> receiveMessages = wiser.getWiser().getMessages(); assertThat(receiveMessages.size(), is(1)); MimeMessage receiveMessage = receiveMessages.iterator().next().getMimeMessage(); assertThat(receiveMessage.getFrom()[0], is(new InternetAddress("[email protected]"))); assertThat(receiveMessage.getAllRecipients()[0], is(new InternetAddress("[email protected]"))); assertThat(receiveMessage.getSubject(), is("ãã¹ã")); assertThat(receiveMessage.getContent(), is("ããã¯ãã¹ãã§ã")); } } }
â ãã®ï¼
package ksbysample.webapp.email.service; import com.icegreen.greenmail.junit.GreenMailRule; import com.icegreen.greenmail.util.GreenMail; import com.icegreen.greenmail.util.GreenMailUtil; import com.icegreen.greenmail.util.ServerSetup; import com.icegreen.greenmail.util.ServerSetupTest; import ksbysample.webapp.email.Application; import ksbysample.webapp.email.test.MailServerWiserResource; import org.junit.Rule; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.SpringApplicationConfiguration; import org.springframework.mail.SimpleMailMessage; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import org.springframework.test.context.web.WebAppConfiguration; import org.subethamail.wiser.WiserMessage; import javax.mail.internet.InternetAddress; import javax.mail.internet.MimeMessage; import java.util.List; import static org.hamcrest.Matchers.is; import static org.junit.Assert.*; public class EmailServiceTest { @RunWith(SpringJUnit4ClassRunner.class) @SpringApplicationConfiguration(classes = Application.class) @WebAppConfiguration public static class Wiserãå©ç¨ãã¦ã¡ã¼ã«éä¿¡ããå ´å { @Rule @Autowired public MailServerWiserResource wiser; @Autowired private EmailService emailService; @Test public void testSendSimpleMail() throws Exception { SimpleMailMessage message = new SimpleMailMessage(); message.setFrom("[email protected]"); message.setTo("[email protected]"); message.setSubject("ãã¹ã"); message.setText("ããã¯ãã¹ãã§ã"); emailService.sendSimpleMail(message); List<WiserMessage> receiveMessages = wiser.getWiser().getMessages(); assertThat(receiveMessages.size(), is(1)); MimeMessage receiveMessage = receiveMessages.iterator().next().getMimeMessage(); assertThat(receiveMessage.getFrom()[0], is(new InternetAddress("[email protected]"))); assertThat(receiveMessage.getAllRecipients()[0], is(new InternetAddress("[email protected]"))); assertThat(receiveMessage.getSubject(), is("ãã¹ã")); assertThat(receiveMessage.getContent(), is("ããã¯ãã¹ãã§ã")); } } @RunWith(SpringJUnit4ClassRunner.class) @SpringApplicationConfiguration(classes = Application.class) @WebAppConfiguration public static class GreenMailãå©ç¨ãã¦ã¡ã¼ã«éä¿¡ããå ´å { @Rule public GreenMailRule greenMail = new GreenMailRule(new ServerSetup(25, "localhost", ServerSetup.PROTOCOL_SMTP)); @Autowired private EmailService emailService; @Test public void testSendSimpleMail() throws Exception { SimpleMailMessage message = new SimpleMailMessage(); message.setFrom("[email protected]"); message.setTo("[email protected]"); message.setSubject("ãã¹ã"); message.setText("ããã¯ãã¹ãã§ã"); emailService.sendSimpleMail(message); MimeMessage[] receivedMessages = greenMail.getReceivedMessages(); assertThat(receivedMessages.length, is(1)); assertThat(receivedMessages[0].getFrom()[0], is(new InternetAddress("[email protected]"))); assertThat(receivedMessages[0].getAllRecipients()[0], is(new InternetAddress("[email protected]"))); assertThat(receivedMessages[0].getSubject(), is("ãã¹ã")); // GreenMailUtil.getBody ã使ç¨ããã¨åå¾ããæ¥æ¬èªã®ã¡ã¼ã«æ¬æã // "44GT44KM44Gv44OG44K544OI44Gn44GZ" // ã§ããããã³ã¼ããå¿ è¦ï¼ //assertThat(GreenMailUtil.getBody(receivedMessages[0]), is("ããã¯ãã¹ãã§ã")); assertThat(receivedMessages[0].getContent(), is("ããã¯ãã¹ãã§ã")); } } }
public static class GreenMailãå©ç¨ãã¦ã¡ã¼ã«éä¿¡ããå ´å { ... }
ã追å ãã¾ãã
â ãã®ï¼
package ksbysample.webapp.email.service; import com.icegreen.greenmail.junit.GreenMailRule; import com.icegreen.greenmail.util.ServerSetup; import ksbysample.webapp.email.Application; import ksbysample.webapp.email.test.MailServerResource; import ksbysample.webapp.email.test.MailServerWiserResource; import org.junit.Rule; import org.junit.Test; import org.junit.experimental.runners.Enclosed; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.SpringApplicationConfiguration; import org.springframework.mail.SimpleMailMessage; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import org.springframework.test.context.web.WebAppConfiguration; import org.subethamail.wiser.WiserMessage; import javax.mail.internet.InternetAddress; import javax.mail.internet.MimeMessage; import java.util.List; import static org.hamcrest.Matchers.is; import static org.junit.Assert.assertThat; @RunWith(Enclosed.class) public class EmailServiceTest { @RunWith(SpringJUnit4ClassRunner.class) @SpringApplicationConfiguration(classes = Application.class) @WebAppConfiguration public static class Wiserãå©ç¨ãã¦ã¡ã¼ã«éä¿¡ããå ´å { @Rule @Autowired public MailServerWiserResource wiser; @Autowired private EmailService emailService; @Test public void testSendSimpleMail() throws Exception { SimpleMailMessage message = new SimpleMailMessage(); message.setFrom("[email protected]"); message.setTo("[email protected]"); message.setSubject("ãã¹ã"); message.setText("ããã¯ãã¹ãã§ã"); emailService.sendSimpleMail(message); List<WiserMessage> receiveMessages = wiser.getWiser().getMessages(); assertThat(receiveMessages.size(), is(1)); MimeMessage receiveMessage = receiveMessages.iterator().next().getMimeMessage(); assertThat(receiveMessage.getFrom()[0], is(new InternetAddress("[email protected]"))); assertThat(receiveMessage.getAllRecipients()[0], is(new InternetAddress("[email protected]"))); assertThat(receiveMessage.getSubject(), is("ãã¹ã")); assertThat(receiveMessage.getContent(), is("ããã¯ãã¹ãã§ã")); } } @RunWith(SpringJUnit4ClassRunner.class) @SpringApplicationConfiguration(classes = Application.class) @WebAppConfiguration public static class GreenMailãå©ç¨ãã¦ã¡ã¼ã«éä¿¡ããå ´å { @Rule public GreenMailRule greenMail = new GreenMailRule(new ServerSetup(25, "localhost", ServerSetup.PROTOCOL_SMTP)); @Autowired private EmailService emailService; @Test public void testSendSimpleMail() throws Exception { SimpleMailMessage message = new SimpleMailMessage(); message.setFrom("[email protected]"); message.setTo("[email protected]"); message.setSubject("ãã¹ã"); message.setText("ããã¯ãã¹ãã§ã"); emailService.sendSimpleMail(message); MimeMessage[] receivedMessages = greenMail.getReceivedMessages(); assertThat(receivedMessages.length, is(1)); assertThat(receivedMessages[0].getFrom()[0], is(new InternetAddress("[email protected]"))); assertThat(receivedMessages[0].getAllRecipients()[0], is(new InternetAddress("[email protected]"))); assertThat(receivedMessages[0].getSubject(), is("ãã¹ã")); // GreenMailUtil.getBody ã使ç¨ããã¨åå¾ããæ¥æ¬èªã®ã¡ã¼ã«æ¬æã // "44GT44KM44Gv44OG44K544OI44Gn44GZ" // ã§ããããã³ã¼ããå¿ è¦ï¼ //assertThat(GreenMailUtil.getBody(receivedMessages[0]), is("ããã¯ãã¹ãã§ã")); assertThat(receivedMessages[0].getContent(), is("ããã¯ãã¹ãã§ã")); } } @RunWith(SpringJUnit4ClassRunner.class) @SpringApplicationConfiguration(classes = Application.class) @WebAppConfiguration public static class MailServerResourceãå©ç¨ãã¦ã¡ã¼ã«éä¿¡ããå ´å { @Rule @Autowired public MailServerResource mailServer; @Autowired private EmailService emailService; @Test public void testSendSimpleMail() throws Exception { SimpleMailMessage message = new SimpleMailMessage(); message.setFrom("[email protected]"); message.setTo("[email protected]"); message.setSubject("ãã¹ã"); message.setText("ããã¯ãã¹ãã§ã"); emailService.sendSimpleMail(message); assertThat(mailServer.getMessagesCount(), is(1)); MimeMessage receiveMessage = mailServer.getFirstMessage(); assertThat(receiveMessage.getFrom()[0], is(new InternetAddress("[email protected]"))); assertThat(receiveMessage.getAllRecipients()[0], is(new InternetAddress("[email protected]"))); assertThat(receiveMessage.getSubject(), is("ãã¹ã")); assertThat(receiveMessage.getContent(), is("ããã¯ãã¹ãã§ã")); } } }
- ãã¹ãã¯ã©ã¹å
ã®ãã¹ããããã¹ãã¯ã©ã¹ãå
¨ã¦ãã¹ããããã¨ãåºæ¥ãªããã¨ã«æ°ã¥ãã¾ããã®ã§ã
@RunWith(Enclosed.class)
ã追å ãã¾ãã public static class MailServerResourceãå©ç¨ãã¦ã¡ã¼ã«éä¿¡ããå ´å { ... }
ã追å ãã¾ãã
MailServerResource.java
package ksbysample.webapp.email.test; import com.icegreen.greenmail.util.GreenMail; import com.icegreen.greenmail.util.ServerSetup; import org.junit.rules.ExternalResource; import org.springframework.stereotype.Component; import javax.mail.internet.MimeMessage; import java.util.Arrays; import java.util.List; @Component public class MailServerResource extends ExternalResource { private GreenMail greenMail = new GreenMail(new ServerSetup(25, "localhost", ServerSetup.PROTOCOL_SMTP)); @Override protected void before() { greenMail.start(); } @Override protected void after() { greenMail.stop(); } public int getMessagesCount() { return greenMail.getReceivedMessages().length; } public List<MimeMessage> getMessages() { return Arrays.asList(greenMail.getReceivedMessages()); } public MimeMessage getFirstMessage() { MimeMessage message = null; MimeMessage[] receivedMessages = greenMail.getReceivedMessages(); if (receivedMessages.length > 0) { message = receivedMessages[0]; } return message; } }
å±¥æ´
2015/05/17
åççºè¡ã