Spring Boot ã§ãã°ã¤ã³ç»é¢ + ä¸è¦§ç»é¢ + ç»é²ç»é¢ã® Webã¢ããªã±ã¼ã·ã§ã³ãä½ã ( ãã®ï¼ï¼ )( unittest ã¢ã¼ããä½æãã¦ã¦ããããã¹ãã®ã¹ãã¼ãã¢ãã )
æ¦è¦
- ä»åã®æé ã§ç¢ºèªã§ããã®ã¯ä»¥ä¸ã®å
容ã§ãã
- unittest ã¢ã¼ããä½æãã¦ã¦ããããã¹ãã®ã¹ãã¼ãã¢ãããå³ãã¾ãã
ã½ããã¦ã§ã¢ä¸è¦§
åèã«ãããµã¤ã
æé
unittest ã¢ã¼ãç¨ã®è¨å®ãã¡ã¤ã«ã®ä½æ
-
IntelliJ IDEA ä¸ã§ 1.0.x-unittestmode ãã©ã³ããä½æãã¾ãã
-
src/main/resources ã®ä¸ã® application-product.properties ãã³ãã¼ã㦠application-unittest.properties ãæ°è¦ä½æãã¾ããå 容㯠application-product.properties ã¨åãã¾ã¾ã«ãã¾ãã
src/main/resources ã®ä¸ã® logback-develop.xml ãã³ãã¼ã㦠logback-unittest.xml ãæ°è¦ä½æãã¾ããä½æå¾ããªã³ã¯å ã®å 容 ã«å¤æ´ãã¾ãã
src/main/resources ã®ä¸ã® logback.xml ã ãªã³ã¯å ã®å 容 ã«å¤æ´ãã¾ãã
Application ã¯ã©ã¹ã®å¤æ´
- src/main/java/ksbysample/webapp/basic ã®ä¸ã® Application.java ã ãªã³ã¯å ã®å 容 ã«å¤æ´ãã¾ãã
ApplicationTest ã¯ã©ã¹ã®å¤æ´
- ApplicationTest ã¯ã©ã¹ã¯ @Ignore ã¢ããã¼ã·ã§ã³ãä»å ãã¦ãã¹ããå®è¡ãããªãããã«ãã¦ãã¾ããã"All Tests Passed" ã®ç·è²ã®ã¡ãã»ã¼ã¸ãåºãªããªãã®ã§ @Ignore ã¢ããã¼ã·ã§ã³ãåé¤ãã¦ã³ã¡ã³ãã¢ã¦ãããæ¹æ³ã«å¤æ´ãã¾ããsrc/test/java/ksbysample/webapp/basic ã®ä¸ã® ApplicationTest.java ã ãªã³ã¯å ã®å 容 ã«å¤æ´ãã¾ãã
ãRun 'Tests in 'ksbysample...' with Coverageãã®ã¢ã¼ãã unitest ã¸å¤æ´
IntelliJ IDEA ã®ã¡ã¤ã³ã¡ãã¥ã¼ãããRunã-ãEdit Configurations...ããé¸æãã¾ãã
ãRun/Debug Configurationsããã¤ã¢ãã°ã表示ããã¾ããç»é¢å·¦å´ã®ãJUnitãã®ä¸ã«è¡¨ç¤ºããã¦ãããAll in ksbysample-webapp-basicãããå§ã¾ãé ç®ãå ¨ã¦åé¤ãã¾ãã
ç»é¢å·¦å´ã®ãDefaultsãã®ä¸ã®ãJUnitããé¸æå¾ãç»é¢å³å´ã®ãVM optionsãã®æååã
-ea -Dspring.profiles.active=develop -Dlog4jdbc.spylogdelegator.name=net.sf.log4jdbc.log.slf4j.Slf4jSpyLogDelegator
â-ea -Dspring.profiles.active=unittest
ã¸å¤æ´ããå¾ããOKããã¿ã³ãã¯ãªãã¯ãã¦ãã¤ã¢ãã°ãéãã¾ãããRun 'Tests in 'ksbysample...' with Coverageããé¸æãã¦ãã¹ããå®è¡ãã¾ããä»åº¦ã¯ãã°ãã»ã¨ãã©åºãã«ãã¹ããçµäºãã¾ããå®è¡æé㯠1å 30ç§ããããã 30ç§ç¨åº¦ã«ç縮ããã¾ããã
Gradle ã® test ã¿ã¹ã¯ã®ã¢ã¼ãã unitest ã¸å¤æ´
build.gradle ã ãªã³ã¯å ã®å 容 ã«å¤æ´ãã¾ãã
test ã¿ã¹ã¯ãå®è¡ãã¾ãããã¡ãããã°ãã»ã¨ãã©åºãã«ãã¹ããçµäºãã¾ãã
commitãGitHub 㸠Pushã1.0.x-unittestmode -> 1.0.x 㸠Pull Requestã1.0.x ã§ãã¼ã¸ã1.0.x-unittestmode ãã©ã³ããåé¤
commit ã®åã« build ã¿ã¹ã¯ãå®è¡ããBUILD SUCCESSFUL ã表示ããããã¨ã確èªãã¾ãã
commitãGitHub 㸠Pushã1.0.x-unittestmode -> 1.0.x 㸠Pull Requestã1.0.x ã§ãã¼ã¸ã1.0.x-unittestmode ãã©ã³ããåé¤ãããã¾ãã
â»commit æã« Code Analysis ã®ãã¤ã¢ãã°ã表示ããã¾ãããApplicationTest ã¯ã©ã¹å ã®è¨è¿°ãå ¨ã¦ã³ã¡ã³ããããã¨ã«ä¼´ã Warning ãªã®ã§ãç¡è¦ãã¦ãCommitããã¿ã³ãã¯ãªãã¯ãã¾ãã
ã½ã¼ã¹ã³ã¼ã
logback-unittest.xml
<?xml version="1.0" encoding="UTF-8"?> <included> <logger name="root" level="OFF"/> <!-- Spring MVC --> <logger name="org.springframework.web" level="OFF"/> <!-- log4jdbc-log4j2 --> <logger name="jdbc.sqlonly" level="OFF"/> <logger name="jdbc.sqltiming" level="OFF"/> <logger name="jdbc.audit" level="OFF"/> <logger name="jdbc.resultset" level="OFF"/> <logger name="jdbc.resultsettable" level="OFF"/> <logger name="jdbc.connection" level="OFF"/> </included>
<logger name="root" level="OFF"/>
ã追å ãã¾ãã- level ãå ¨ã¦ OFF ã«è¨å®ãã¾ãã
logback.xml
<?xml version="1.0" encoding="UTF-8"?> <configuration> <include resource="org/springframework/boot/logging/logback/base.xml"/> <if condition='"${spring.profiles.active}" == "develop"'> <then> <include resource="logback-develop.xml"/> </then> </if> <if condition='"${spring.profiles.active}" == "unittest"'> <then> <include resource="logback-unittest.xml"/> </then> </if> <if condition='"${spring.profiles.active}" == "product"'> <then> <include resource="logback-product.xml"/> </then> </if> </configuration>
<if condition='"${spring.profiles.active}" == "unittest"'> ... </if>
ã®è¨è¿°ã追å ãã¾ãã
Application.java
package ksbysample.webapp.basic; import org.apache.commons.lang3.StringUtils; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import java.text.MessageFormat; @SpringBootApplication public class Application { public static void main(String[] args) { String springProfilesActive = System.getProperty("spring.profiles.active"); if (!StringUtils.equals(springProfilesActive, "product") && !StringUtils.equals(springProfilesActive, "develop") && !StringUtils.equals(springProfilesActive, "unittest")) { throw new UnsupportedOperationException(MessageFormat.format("JVMã®èµ·åæå¼æ° -Dspring.profiles.active 㧠develop ã unittest ã product ãæå®ãã¦ä¸ãã ( -Dspring.profiles.active={0} )ã", springProfilesActive)); } SpringApplication.run(Application.class, args); } }
- if æã®æ¡ä»¶ã«
&& !StringUtils.equals(springProfilesActive, "unittest")
ã追å ãã¾ãã - ã¨ã©ã¼æã®ã¡ãã»ã¼ã¸ã«
ã unittest
ã追å ãã¾ãã
ApplicationTest.java
package ksbysample.webapp.basic; import org.junit.Ignore; import org.junit.Rule; import org.junit.Test; import org.springframework.boot.test.OutputCapture; import static org.hamcrest.CoreMatchers.is; import static org.junit.Assert.assertThat; public class ApplicationTest { /* @Rule public OutputCapture outputCapture = new OutputCapture(); @Test public void testApplicationProductMode() { String SPRING_PROFILES_ACTIVE = System.getProperty("spring.profiles.active"); System.setProperty("spring.profiles.active", "product"); Application.main(new String[]{"--server.port=8081"}); String output = this.outputCapture.toString(); assertThat(output.contains("Started Application"), is(true)); System.setProperty("spring.profiles.active", SPRING_PROFILES_ACTIVE); } @Test public void testApplicationDevelopMode() { String SPRING_PROFILES_ACTIVE = System.getProperty("spring.profiles.active"); System.setProperty("spring.profiles.active", "develop"); Application.main(new String[]{"--server.port=8082"}); String output = this.outputCapture.toString(); assertThat(output.contains("Started Application"), is(true)); System.setProperty("spring.profiles.active", SPRING_PROFILES_ACTIVE); } @Test public void testApplicationEmptyMode() { String SPRING_PROFILES_ACTIVE = System.getProperty("spring.profiles.active"); System.setProperty("spring.profiles.active", ""); try { Application.main(new String[]{"--server.port=8083"}); } catch (Exception e) { e.printStackTrace(); } String output = this.outputCapture.toString(); assertThat(output.contains("Started Application"), is(false)); assertThat(output.contains("JVMã®èµ·åæå¼æ° -Dspring.profiles.active 㧠develop ã product ãæå®ãã¦ä¸ãã"), is(true)); System.setProperty("spring.profiles.active", SPRING_PROFILES_ACTIVE); } */ }
- ã¯ã©ã¹å ã®è¨è¿°ãå ¨ã¦ / / ã§å²ãã§ã³ã¡ã³ãã¢ã¦ããã¾ãã
build.gradle
test {
jvmArgs = ['-Dspring.profiles.active=unittest']
}
- spring.profiles.active ã«æå®ããæååã
develop
âunittest
ãå¤æ´ãã¾ãã
å±¥æ´
2015/03/17
åççºè¡ã