Spring Boot 2.0.x ã® Web ã¢ããªã 2.1.x ã¸ãã¼ã¸ã§ã³ã¢ãããã ( ãã®ï¼ï¼ )( @Rule ã使ç¨ãã¦ãããã¹ãã JUnit 5 ã®ãã¹ãã«æ¸ãç´ã )
æ¦è¦
è¨äºä¸è¦§ã¯ãã¡ãã§ãã
- ä»åã®æé ã§ç¢ºèªã§ããã®ã¯ä»¥ä¸ã®å
容ã§ãã
- ãã¹ãã¯ã©ã¹å 㧠@Rule ã¢ããã¼ã·ã§ã³ãä»ä¸ãã¦ããã¯ã©ã¹ã JUnit 5 ã® Extension Model ã¨ãã¦ä½¿ç¨ã§ããããã«æ¸ãç´ãã¦ããã@Rule ã使ç¨ãã¦ãããã¹ãã JUnit 5 ã®ãã¹ãã«æ¸ãç´ãã¾ãã
åç §ãããµã¤ãã»æ¸ç±
-
JUnit 5 User Guide - 5. Extension Model
https://junit.org/junit5/docs/snapshot/user-guide/#extensions TestWatcher in junit5
https://stackoverflow.com/questions/49037406/testwatcher-in-junit5
ç®æ¬¡
- æ¹é
- MailServerResource ã¯ã©ã¹ã JUnit 5 ã® Extension ã¨ãã¦ã使ç¨ã§ããããã«ãã
- SecurityMockMvcResource ã¯ã©ã¹ã JUnit 5 ã® Extension ã¨ãã¦ã使ç¨ã§ããããã«ãã
- 次åã¸ç¶ãããã
æé
æ¹é
ãã¹ãã¯ã©ã¹å
㧠@Rule
ã¢ããã¼ã·ã§ã³ãä»ä¸ãã¦ããã¯ã©ã¹ã¯ä»¥ä¸ã®ï¼ã¤ã§ããã
- src/test/java/ksbysample/common/test/rule/mail/MailServerResource.javaãExternalResource ã¯ã©ã¹ãç¶æ¿ã㦠before ã¡ã½ãã㨠after ã¡ã½ããã override ãã¦ãã¾ãã
- src/test/java/ksbysample/common/test/rule/mockmvc/SecurityMockMvcResource.javaãExternalResource ã¯ã©ã¹ãç¶æ¿ã㦠before ã¡ã½ãã㨠after ã¡ã½ããã override ãã¦ãã¾ãã
- src/test/java/ksbysample/common/test/rule/db/TestDataResource.javaãTestWatcher ã¯ã©ã¹ãç¶æ¿ã㦠starting ã¡ã½ãã㨠finished ã¡ã½ããã override ãã¦ãã¾ãã
ãããã以ä¸ã®æ¹éã§å¤æ´ãã¾ãã
- ï½Resource ã¨ããã¯ã©ã¹åã«ãã¦ãã¾ãã ï½Extension ã¨ããã¯ã©ã¹åã«å¤æ´ãã¾ããã¾ã package ã ksbysample.common.test.rule ãã ksbysample.common.test.extension ã«å¤æ´ãã¾ãã
- JUnit 4 ã®
@Rule
ã§ã JUnit 5 ã® Extension ã§ãã©ã¡ãã§ã使ç¨ã§ããããã«å¤æ´ãã¾ãã
MailServerResource ã¯ã©ã¹ã JUnit 5 ã® Extension ã¨ãã¦ã使ç¨ã§ããããã«ãã
ã¾ã㯠src/test/java/ksbysample/common/test/rule/mail/MailServerResource.java ãããç¾å¨ä»¥ä¸ã®å®è£ ã§ããã
package ksbysample.common.test.rule.mail; 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; } }
ããã以ä¸ã®ããã«å¤æ´ãã¾ãã
package ksbysample.common.test.extension.mail; import com.icegreen.greenmail.util.GreenMail; import com.icegreen.greenmail.util.ServerSetup; import org.junit.jupiter.api.extension.AfterEachCallback; import org.junit.jupiter.api.extension.BeforeEachCallback; import org.junit.jupiter.api.extension.ExtensionContext; 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 MailServerExtension extends ExternalResource implements BeforeEachCallback, AfterEachCallback { private GreenMail greenMail = new GreenMail(new ServerSetup(25, "localhost", ServerSetup.PROTOCOL_SMTP)); @Override protected void before() { greenMail.start(); } @Override protected void after() { greenMail.stop(); } @Override public void beforeEach(ExtensionContext context) { before(); } @Override public void afterEach(ExtensionContext context) { after(); } 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; } }
- package ã
ksbysample.common.test.rule.mail
âksbysample.common.test.extension.mail
ã«å¤æ´ãã¾ãï¼ãã¡ã¤ã«ã移åãã¾ãï¼ã - ã¯ã©ã¹åã
MailServerResource
âMailServerExtension
ã«å¤æ´ãã¾ãã implements BeforeEachCallback, AfterEachCallback
ã追å ãã¾ãã- beforeEach ã¡ã½ããã override ããä¸ã§ before ã¡ã½ãããå¼ã³åºãã¾ãã
- afterEach ã¡ã½ããã override ããä¸ã§ after ã¡ã½ãããå¼ã³åºãã¾ãã
æåã« JUnit 4 ã®ãã¹ãã§æ£å¸¸ã«åä½ãããã¨ã確èªãã¾ããsrc/test/java/ksbysample/webapp/lending/helper/mail/EmailHelperTest.java 㯠JUnit 4 ã®ãã¹ãã¨ãã¦ä»¥ä¸ã®ããã«å®è£ ããã¦ãã¾ããã
package ksbysample.webapp.lending.helper.mail; import ksbysample.common.test.extension.mail.MailServerExtension; 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.context.SpringBootTest; import org.springframework.mail.SimpleMailMessage; import org.springframework.mail.javamail.JavaMailSender; import org.springframework.mail.javamail.MimeMessageHelper; import org.springframework.test.context.junit4.SpringRunner; import javax.mail.internet.InternetAddress; import javax.mail.internet.MimeMessage; import static org.hamcrest.Matchers.is; import static org.junit.Assert.assertThat; @RunWith(SpringRunner.class) @SpringBootTest public class EmailHelperTest { @Rule @Autowired public MailServerExtension mailServer; @Autowired private JavaMailSender mailSender; @Autowired private EmailHelper emailHelper; @Test public void testSendSimpleMail() throws Exception { SimpleMailMessage message = new SimpleMailMessage(); message.setFrom("[email protected]"); message.setTo("[email protected]"); message.setSubject("ãã¹ã"); message.setText("ããã¯ãã¹ãã§ã"); emailHelper.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("ããã¯ãã¹ãã§ã")); } @Test public void testSendMail() throws Exception { MimeMessage mimeMessage = mailSender.createMimeMessage(); MimeMessageHelper message = new MimeMessageHelper(mimeMessage); message.setFrom("[email protected]"); message.setTo("[email protected]"); message.setSubject("ãã¹ã"); message.setText("ããã¯ãã¹ãã§ã"); emailHelper.sendMail(message.getMimeMessage()); 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("ããã¯ãã¹ãã§ã")); } }
ãããå®è¡ããã¨ãã¹ãã¯æåãã¾ãã
次㫠JUnit 5 ã§ä»¥ä¸ã®ããã«æ¸ãç´ãã¦ããã
package ksbysample.webapp.lending.helper.mail; import ksbysample.common.test.extension.mail.MailServerExtension; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.RegisterExtension; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.mail.SimpleMailMessage; import org.springframework.mail.javamail.JavaMailSender; import org.springframework.mail.javamail.MimeMessageHelper; import javax.mail.internet.InternetAddress; import javax.mail.internet.MimeMessage; import static org.assertj.core.api.Assertions.assertThat; import static org.junit.jupiter.api.Assertions.assertAll; @SpringBootTest public class EmailHelperTest { @RegisterExtension @Autowired public MailServerExtension mailServer; @Autowired private JavaMailSender mailSender; @Autowired private EmailHelper emailHelper; @Test void testSendSimpleMail() throws Exception { SimpleMailMessage message = new SimpleMailMessage(); message.setFrom("[email protected]"); message.setTo("[email protected]"); message.setSubject("ãã¹ã"); message.setText("ããã¯ãã¹ãã§ã"); emailHelper.sendSimpleMail(message); assertThat(mailServer.getMessagesCount()).isEqualTo(1); MimeMessage receiveMessage = mailServer.getFirstMessage(); assertAll( () -> assertThat(receiveMessage.getFrom()[0]).isEqualTo(new InternetAddress("[email protected]")), () -> assertThat(receiveMessage.getAllRecipients()[0]).isEqualTo(new InternetAddress("[email protected]")), () -> assertThat(receiveMessage.getSubject()).isEqualTo("ãã¹ã"), () -> assertThat(receiveMessage.getContent()).isEqualTo("ããã¯ãã¹ãã§ã") ); } @Test void testSendMail() throws Exception { MimeMessage mimeMessage = mailSender.createMimeMessage(); MimeMessageHelper message = new MimeMessageHelper(mimeMessage); message.setFrom("[email protected]"); message.setTo("[email protected]"); message.setSubject("ãã¹ã"); message.setText("ããã¯ãã¹ãã§ã"); emailHelper.sendMail(message.getMimeMessage()); assertThat(mailServer.getMessagesCount()).isEqualTo(1); MimeMessage receiveMessage = mailServer.getFirstMessage(); assertAll( () -> assertThat(receiveMessage.getFrom()[0]).isEqualTo(new InternetAddress("[email protected]")), () -> assertThat(receiveMessage.getAllRecipients()[0]).isEqualTo(new InternetAddress("[email protected]")), () -> assertThat(receiveMessage.getSubject()).isEqualTo("ãã¹ã"), () -> assertThat(receiveMessage.getContent()).isEqualTo("ããã¯ãã¹ãã§ã") ); } }
import org.junit.Test;
âimport org.junit.jupiter.api.Test;
ã«å¤æ´ãã¾ãã@RunWith(SpringRunner.class)
ãåé¤ãã¾ãã@Rule
â@RegisterExtension
ã«å¤æ´ãã¾ãã- ãã¹ãã¡ã½ããã®
public
ãåé¤ãã¾ãã MimeMessage receiveMessage = mailServer.getFirstMessage();
ãå¼ã³åºããå¾ã®ã¡ã¼ã«ã®å 容ã確èªãã¦ããé¨åã assertAll ã使ç¨ããããå¤æ´ãã¾ããã¾ã assertThat 㯠JUnit 4 ã® org.junit.Assert.assertThat ã ã£ãã®ã§ AssertJ ã® assertThat ã«å¤æ´ãã¾ãã
å®è¡ããã¨ããã¡ãããã¹ããæåãã¾ãã
SecurityMockMvcResource ã¯ã©ã¹ã JUnit 5 ã® Extension ã¨ãã¦ã使ç¨ã§ããããã«ãã
src/test/java/ksbysample/common/test/rule/mockmvc/SecurityMockMvcResource.java ã¯ä»¥ä¸ã®å®è£ ã§ããã
package ksbysample.common.test.rule.mockmvc; import org.junit.rules.ExternalResource; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication; import org.springframework.security.core.userdetails.UserDetails; import org.springframework.security.core.userdetails.UserDetailsService; import org.springframework.stereotype.Component; import org.springframework.test.web.servlet.MockMvc; import org.springframework.test.web.servlet.setup.MockMvcBuilders; import org.springframework.web.context.WebApplicationContext; import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.user; import static org.springframework.security.test.web.servlet.setup.SecurityMockMvcConfigurers.springSecurity; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; @Component @ConditionalOnWebApplication public class SecurityMockMvcResource extends ExternalResource { public final String MAILADDR_TANAKA_TARO = "[email protected]"; public final String MAILADDR_SUZUKI_HANAKO = "[email protected]"; public final String MAILADDR_KIMURA_MASAO = "[email protected]"; public final String MAILADDR_ENDO_YOKO = "[email protected]"; public final String MAILADDR_SATO_MASAHIKO = "[email protected]"; public final String MAILADDR_TAKAHASI_NAOKO = "[email protected]"; public final String MAILADDR_ITO_AOI = "[email protected]"; @Autowired private WebApplicationContext context; @Autowired private UserDetailsService userDetailsService; public MockMvc authTanakaTaro; public MockMvc authSuzukiHanako; public MockMvc authItoAoi; public MockMvc noauth; @Override protected void before() throws Throwable { // èªè¨¼ã¦ã¼ã¶ç¨MockMvc ( user = [email protected] ) UserDetails userDetailsTanakaTaro = userDetailsService.loadUserByUsername(MAILADDR_TANAKA_TARO); this.authTanakaTaro = MockMvcBuilders.webAppContextSetup(this.context) .defaultRequest(get("/").with(user(userDetailsTanakaTaro))) .apply(springSecurity()) .build(); // èªè¨¼ã¦ã¼ã¶ç¨MockMvc ( user = [email protected] ) UserDetails userDetailsSuzukiHanako = userDetailsService.loadUserByUsername(MAILADDR_SUZUKI_HANAKO); this.authSuzukiHanako = MockMvcBuilders.webAppContextSetup(this.context) .defaultRequest(get("/").with(user(userDetailsSuzukiHanako))) .apply(springSecurity()) .build(); // èªè¨¼ã¦ã¼ã¶ç¨MockMvc ( user = [email protected] ) UserDetails userDetailsItoAoi = userDetailsService.loadUserByUsername(MAILADDR_ITO_AOI); this.authItoAoi = MockMvcBuilders.webAppContextSetup(this.context) .defaultRequest(get("/").with(user(userDetailsItoAoi))) .apply(springSecurity()) .build(); // éèªè¨¼ã¦ã¼ã¶ç¨MockMvc this.noauth = MockMvcBuilders.webAppContextSetup(this.context) .apply(springSecurity()) .build(); } }
ããã以ä¸ã®ããã«å¤æ´ãã¾ãã
package ksbysample.common.test.extension.mockmvc; import org.junit.jupiter.api.extension.BeforeEachCallback; import org.junit.jupiter.api.extension.ExtensionContext; import org.junit.rules.ExternalResource; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication; import org.springframework.security.core.userdetails.UserDetails; import org.springframework.security.core.userdetails.UserDetailsService; import org.springframework.stereotype.Component; import org.springframework.test.web.servlet.MockMvc; import org.springframework.test.web.servlet.setup.MockMvcBuilders; import org.springframework.web.context.WebApplicationContext; import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.user; import static org.springframework.security.test.web.servlet.setup.SecurityMockMvcConfigurers.springSecurity; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; @Component @ConditionalOnWebApplication public class SecurityMockMvcExtension extends ExternalResource implements BeforeEachCallback { public final String MAILADDR_TANAKA_TARO = "[email protected]"; public final String MAILADDR_SUZUKI_HANAKO = "[email protected]"; public final String MAILADDR_KIMURA_MASAO = "[email protected]"; public final String MAILADDR_ENDO_YOKO = "[email protected]"; public final String MAILADDR_SATO_MASAHIKO = "[email protected]"; public final String MAILADDR_TAKAHASI_NAOKO = "[email protected]"; public final String MAILADDR_ITO_AOI = "[email protected]"; @Autowired private WebApplicationContext context; @Autowired private UserDetailsService userDetailsService; public MockMvc authTanakaTaro; public MockMvc authSuzukiHanako; public MockMvc authItoAoi; public MockMvc noauth; @Override protected void before() { // èªè¨¼ã¦ã¼ã¶ç¨MockMvc ( user = [email protected] ) UserDetails userDetailsTanakaTaro = userDetailsService.loadUserByUsername(MAILADDR_TANAKA_TARO); this.authTanakaTaro = MockMvcBuilders.webAppContextSetup(this.context) .defaultRequest(get("/").with(user(userDetailsTanakaTaro))) .apply(springSecurity()) .build(); // èªè¨¼ã¦ã¼ã¶ç¨MockMvc ( user = [email protected] ) UserDetails userDetailsSuzukiHanako = userDetailsService.loadUserByUsername(MAILADDR_SUZUKI_HANAKO); this.authSuzukiHanako = MockMvcBuilders.webAppContextSetup(this.context) .defaultRequest(get("/").with(user(userDetailsSuzukiHanako))) .apply(springSecurity()) .build(); // èªè¨¼ã¦ã¼ã¶ç¨MockMvc ( user = [email protected] ) UserDetails userDetailsItoAoi = userDetailsService.loadUserByUsername(MAILADDR_ITO_AOI); this.authItoAoi = MockMvcBuilders.webAppContextSetup(this.context) .defaultRequest(get("/").with(user(userDetailsItoAoi))) .apply(springSecurity()) .build(); // éèªè¨¼ã¦ã¼ã¶ç¨MockMvc this.noauth = MockMvcBuilders.webAppContextSetup(this.context) .apply(springSecurity()) .build(); } @Override public void beforeEach(ExtensionContext context) { before(); } }
- package ã
ksbysample.common.test.rule.mockmvc
âksbysample.common.test.extension.mockmvc
ã«å¤æ´ãã¾ãã - ã¯ã©ã¹åã
SecurityMockMvcResource
âSecurityMockMvcExtension
ã«å¤æ´ãã¾ãã implements BeforeEachCallback
ã追å ãã¾ãã- before ã¡ã½ããã®
throws Throwable
ãåé¤ãã¾ãã - beforeEach ã¡ã½ããã override ããä¸ã§ before ã¡ã½ãããå¼ã³åºãã¾ãã
ç¾å¨ä½ææ¸ã®ãã¹ã㧠SecurityMockMvcExtension ã使ç¨ãã¦ãããã®ã¯ TestDataResource ãä¸ç·ã«ä½¿ç¨ãã¦ããã®ã§ãTestDataResource ãå¤æ´ãã¦ããåä½ç¢ºèªãã¾ãã
次åã¸ç¶ãããã
é·ããªã£ãã®ã§æ¬¡åã¸ç¶ãã¾ãã
å±¥æ´
2019/03/09
åççºè¡ã