Spring Boot ã§æ¸ç±ã®è²¸åºç¶æ³ç¢ºèªã»è²¸åºç³è«ãã Web ã¢ããªã±ã¼ã·ã§ã³ãä½ã ( çªå¤ç·¨ )( Spring Boot ã 1.2.7 â 1.2.8 ã¸ãã¼ã¸ã§ã³ã¢ãã )
æ¦è¦
- Spring Boot ã® 1.2.8 ã«ãã¼ã¸ã§ã³ã¢ãããã¾ãã1.3 ç³»ã«ã¯ãã¼ã¸ã§ã³ã¢ãããã¾ããã
- Spring IO platform ã使ç¨ã§ãã¦ããªããã¨ã«æ°ã¥ããã®ã§ã使ç¨ããããä¿®æ£ãã¦ã¿ã¾ãã
- ãã¼ã¸ã§ã³ãææ°ã§ãªãã©ã¤ãã©ãªãææ°ã«ãã¾ãã
åç §ãããµã¤ãã»æ¸ç±
-
Spring IO platform
http://platform.spring.io/platform/ -
Spring IO Platform Reference Guide
http://docs.spring.io/platform/docs/1.1.5.RELEASE/reference/htmlsingle/ -
Better dependency management for Gradle
https://spring.io/blog/2015/02/23/better-dependency-management-for-gradle Class RuleChain
http://junit.org/apidocs/org/junit/rules/RuleChain.htmlGradle 㧠BOM ã使ãããã¨ãã«ã¯ Spring ãã¼ã ã®åºãã¦ãã Dependency management plugin ã使ãã®ããããã
http://create-something.hatenadiary.jp/entry/2015/05/08/063000
ç®æ¬¡
- feature/51-issue ãã©ã³ãã®ä½æ
- Spring Boot ã 1.2.7 â 1.2.8 ã¸ãã¼ã¸ã§ã³ã¢ãããã
- Spring IO platform ã使ç¨ãããã¨ãããããã
- TestDataResource ã¯ã©ã¹ã®ä¿®æ£
- ãã¼ã¸ã§ã³ãææ°ã§ãªãã©ã¤ãã©ãªãææ°ã«ãã
- PushãPull Requestããã¼ã¸
- 次åã¯ããã
æé
feature/51-issue ãã©ã³ãã®ä½æ
- feature/51-issue ãã©ã³ããä½æãã¾ãã
Spring Boot ã 1.2.7 â 1.2.8 ã¸ãã¼ã¸ã§ã³ã¢ãããã
æåã« spring-boot-gradle-plugin ã«æå®ãããã¼ã¸ã§ã³ã¨ Spring Security ã®ãã¼ã¸ã§ã³ãå¤æ´ãã¾ããbuild.gradle ã ãªã³ã¯å ã®ãã®ï¼ã®å 容 ã«å¤æ´ãã¾ãã
Gradle projects View ã®å·¦ä¸ã«ãããRefresh all Gradle projectsããã¿ã³ãã¯ãªãã¯ãã¦æ´æ°ãã¾ãã
Project View ã® External Libraries 㧠Spring Boot ã 1.2.8.RELEASE ã«ãªã£ã¦ãããã¨ã確èªã§ãã¾ãã
clean ã¿ã¹ã¯å®è¡ â Rebuild Project å®è¡ããã¾ãã
ãorg.springframework.test.utilã®org.springframework.test.util.MatcherAssertionErrorsã¯éæ¨å¥¨ã«ãªãã¾ãããã¨ããã¨ã©ã¼ã¡ãã»ã¼ã¸ã表示ããã¾ããã
src/test/java/ksbysample/common/test ã®ä¸ã® CustomModelResultMatchers.java ã ãªã³ã¯å ã®å 容 ã«å¤æ´ãã¾ãã
å度 Rebuild Project ãå®è¡ãã¦ã¨ã©ã¼ãåºãã«å®äºããã®ã§ãbuild ã¿ã¹ã¯ãå®è¡ãã¾ãã
åé¡ãªã "BUILD SUCCESSFUL" ãåºåããã¾ããã
Project View ãã Run 'All Tests' with Coverage ãå®è¡ãã¦ã¿ã¾ãã
ãã¹ãã¯å ¨ã¦æåãã¾ããã
ä¸æ¦ commit ãã¾ãã
Spring IO platform ã使ç¨ãããã¨ãããããã
build.gradle ã« dependency-management-plugin ãè¨è¿°ãã¦ããã®ã§ãã io.spring.platform:platform-bom
ãè¨è¿°ãã¦ãã¾ããã§ãããio.spring.platform:platform-bom
ãè¨è¿°ããSpring IO platform ã«ãã¼ã¸ã§ã³ã®æå®ãä»»ããã©ã¤ãã©ãªã¯ dependencies ã¿ã¹ã¯ã§ãã¼ã¸ã§ã³ãè¨è¼ããªãããã«ãã¾ãã
ã¾ãã¯
io.spring.platform:platform-bom
ã®è¨è¿°ã ã追å ãã¦åé¡ãªãã確èªãã¾ããbuild.gradle ã ãªã³ã¯å ã®ãã®ï¼ã®å 容 ã«å¤æ´ãã¾ããGradle projects View ã®å·¦ä¸ã«ãããRefresh all Gradle projectsããã¿ã³ãã¯ãªãã¯ãã¦æ´æ°ãã¾ãã
Project View ã® External Libraries 㧠Spring Boot ã®ãã¼ã¸ã§ã³ã¯ 1.2.8.RELEASE ã®ã¾ã¾ã§ç¹ã«å¤æ´ã¯ãªãããã§ãã
clean ã¿ã¹ã¯å®è¡ â Rebuild Project å®è¡ããã¦ã¿ã¾ããããç¹ã«ã¨ã©ã¼ã¯çºçãã¾ããã§ããã
build ã¿ã¹ã¯ãå®è¡ãã¾ãããã10 åã®ãã¹ãã§ã¨ã©ã¼ãçºçãã¾ãããåå ã調æ»ãã¾ãã
調æ»ã®çµæãåãã£ãã®ã¯ä»¥ä¸ã®ç¹ã§ããã
- ã¨ã©ã¼ãçºçãã¦ããã®ã¯å ¨ã¦ @TestDataLoader ã¢ããã¼ã·ã§ã³ãä»å ãã¦ãã¹ããã¼ã¿ãæå ¥ãã¦ãããã®ã§ããã
io.spring.platform:platform-bom
ãè¨è¿°ããå㯠TestDataResource ã«ããããã¯ã¢ããï¼åæãã¼ã¿æå ¥ â TestDataLoaderResource ã«ãããã¹ããã¼ã¿æå ¥ ã¨ããé çªã§å¦çããã¦ããã®ã§ãããio.spring.platform:platform-bom
ãè¨è¿°ããå¾ã¯ TestDataLoaderResource â TestDataResource ã¨ãã以åã¨éã®é çªã§å¦çããã¦ãã¾ããã- å®ã¯ä»¥å èªä½ãããã¹ãç¨ã¯ã©ã¹ ( src/test/java/ksbysample/common/test ) ã®ä½¿ãæ¹ ãæ¸ããæã«ãJUnit ã® Rule ã¯å¦çé åºã¯æ±ºã¾ã£ã¦ãããå®è¡é åºãå¶å¾¡ãããå ´åã«ã¯ RuleChain ã使ç¨ããå¿ è¦ãããã¨ããä»æ§ã«æ°ã¥ãã¦ããã®ã§ããããªãããã¾ãåãã¦ããã®ã§ç¡è¦ãã¦ãããã§ãããããã
io.spring.platform:platform-bom
ãè¨è¿°ããåã¨å¾ã§ junit ã®ãã¼ã¸ã§ã³ãã©ãå¤ããã®ãã確èªãã¦ã¿ã¾ãããè¨è¿°åã¯ã 㨠4.12 ã§ããããè¨è¿°å¾ã¯ã 㨠4.11 ã§ãã¼ã¸ã§ã³ãã¦ã³ãã¦ãã¾ããã4.12 ããªãªã¼ã¹ãããã®ã¯ 2014/12/4 ãªã®ã§ 4.12 ã«ãªã£ã¦ãã¦æ¬²ããã£ãã®ã§ããã4.12 ã«ãªãã®ã¯ Spring IO platform ã® 2.0ç³»ããã®ããã§ã ( 2.0.1 ã® Reference Guide ãè¦ã㨠junit 4.12 ã§ãã )ã- Spring Boot 1.2.8 ã® Appendix E. Dependency versions ãè¦ã㨠junit ã®ãã¼ã¸ã§ã³ã¯ 4.12 ãªã®ã§ãããSpring IO Platform ã® 1.1.5 ã® Appendix A. Dependency versions ãè¦ã㨠junit ã®ãã¼ã¸ã§ã³ã¯ 4.11 ã§ããã
以ä¸ã®ããã«å¯¾å¿ãã¾ãã
- junit 以å¤ã«ããã¼ã¸ã§ã³ãã¦ã³ãã¦ãã¾ãã©ã¤ãã©ãªããã Spring IO platform ã使ç¨ããã¡ãªãããæããããªãã®ã§
build.gradle ã«
io.spring.platform:platform-bom
ã®è¨è¿°ãå ¥ããªããã¨ã«ãã¾ããSpring Boot ã® 1.3ç³»ã使ç¨ããæã«æ¹ãã¦æ¤è¨¼ãã¾ãã - TestDataResource 㨠TestDataLoaderResource ã®ï¼ã¤ã«åãã¦ããã®ã¯åé¡ã®ãããªã®ã§ãTestDataResource ã«ãã¹ããã¼ã¿ãã¼ãã®æ©è½ãæãããããã«ãã¾ãã
- junit 以å¤ã«ããã¼ã¸ã§ã³ãã¦ã³ãã¦ãã¾ãã©ã¤ãã©ãªããã Spring IO platform ã使ç¨ããã¡ãªãããæããããªãã®ã§
build.gradle ã«
build.gradle ã«è¿½å ãã
dependencyManagement { ... }
ã®è¨è¿°ãåé¤ããå¾ãGradle projects View ã®å·¦ä¸ã«ãããRefresh all Gradle projectsããã¿ã³ãã¯ãªãã¯ãã¦æ´æ°ãã¦å ã«æ»ãã¾ãã
TestDataResource ã¯ã©ã¹ã®ä¿®æ£
å®è£ æ¹å¼ã決ãã¾ãã
- TestDataResource ã¯ã©ã¹ã¯ ExternalResource ã¯ã©ã¹ã®ç¶æ¿ã¯ã©ã¹ãTestDataLoaderResource ã¯ã©ã¹ã¯ TestWatcher ã¯ã©ã¹ã®ç¶æ¿ã¯ã©ã¹ã§ããã
- ãã¹ããã¼ã¿ãæå®ãã @TestDataLoader ã¢ããã¼ã·ã§ã³ã«ã¤ãã¦ã¯ç¹ã«ä¸æºãæãã¦ããªãã®ã§ããã®ã¾ã¾ä½¿ç¨ãããã
- ãããã㨠TestDataResource ã¯ã©ã¹ã« TestDataLoaderResource ã¯ã©ã¹ã®ãã¹ããã¼ã¿ãã¼ãæ©è½ãæãããããã«ã¯ãã¹ãã¡ã½ããã«ä»å ãããã¢ããã¼ã·ã§ã³ãå¤å¥ããããã®
Description description
ãå¿ è¦ã - ãªã ExternalResource ã¯ã©ã¹ã®ç¶æ¿ã¯ã©ã¹ã® before ã¡ã½ããã«ã¯
Description description
ããªãã®ã ExternalResource ã¯ã©ã¹ã¨ TestWatcher ã¯ã©ã¹ã®ã½ã¼ã¹ã確èªãã¦ã¿ãã¨ããã- ExternalResource ã¯ã©ã¹ã¨ TestWatcher ã¯ã©ã¹ã¯ã©ã¡ãã TestRule ã¤ã³ã¿ã¼ãã§ã¼ã¹ãå®è£ ããæ½è±¡ã¯ã©ã¹ã§ããã
- TestRule ã¤ã³ã¿ã¼ãã§ã¼ã¹ã«å®ç¾©ããã¦ããã¡ã½ãã㯠apply ã®ã¿ãapply ã¡ã½ããã®å¼æ°ã«ã¯
Description description
ãããã - ExternalResource ã¯ã©ã¹ã¨ TestWatcher ã¯ã©ã¹ã® apply ã¡ã½ããã®å®è£
ãæ¯è¼ãã¦ã¿ãã¨ãããExternalResource.before 㨠TestWatcher.startingãExternalResource.after 㨠TestWatcher.finished ãå®è¡ã¿ã¤ãã³ã°ãåãã§ãéãã¯å¼æ°
Description description
ã®æç¡ãä¾å¤çºçæã®å¦çãç°ãªãç¨åº¦ã§ãã£ãã
- ã§ããã° TestDataResource ã®ç¶æ¿å ã ExternalResource â TestWatcher ã¸å¤æ´ãã¦ãå®è£ ãä¸é¨ä¿®æ£ããã°ããããã§ãã
- TestDataLoaderResource ã¯ã©ã¹ã¯åé¤ãã¾ããããã¹ããã¼ã¿ããã¼ãããã ãã®ã¯ã©ã¹ã¯ããã¨ä¾¿å©ãªã®ã§ã¦ã¼ãã£ãªãã£ã¯ã©ã¹ã«å¤æ´ãã¾ãã
- ã¦ã¼ãã£ãªãã£ã¯ã©ã¹ã®ã¯ã©ã¹åã TestDataLoader ã«ãããã®ã§ããã¹ããã¼ã¿ãæå®ããã¢ããã¼ã·ã§ã³ã @TestDataLoader â @TestData ã¸å¤æ´ãã¾ãã
src/test/java/ksbysample/common/test ã®ä¸ã® TestDataLoader.java ãé¸æãã¦ã³ã³ããã¹ãã¡ãã¥ã¼ãããRefactorã-ãRenameããé¸æããå¾ããRenameããã¤ã¢ãã°ã§ã¯ã©ã¹åã "TestData" ã«å¤æ´ãã¦ãOKããã¿ã³ãã¯ãªãã¯ãã¾ãã
ãRename Variablesããã¤ã¢ãã°ã表示ãããã®ã§ãä¸ã®ãªã¹ãã®ã¢ã¤ãã ããã§ãã¯ããå¾ãOKããã¿ã³ãã¯ãªãã¯ãã¾ãã
以ä¸ã®ã½ã¼ã¹å ã®
@Rule @Autowired public TestDataLoaderResource testDataLoaderResource;
ã®è¨è¿°ãåé¤ãã¾ãã- src/test/java/ksbysample/webapp/lending/listener/rabbitmq/InquiringStatusOfBookQueueListenerTest.java
- src/test/java/ksbysample/webapp/lending/web/admin/library/AdminLibraryControllerTest.java
- src/test/java/ksbysample/webapp/lending/web/admin/library/AdminLibraryServiceTest.java
- src/test/java/ksbysample/webapp/lending/web/lendingapp/LendingappControllerTest.java
ä½æ¥ã¨ãã¦ã¯ IntelliJ IDEA 㧠Ctrl+SHIFT+F ãæ¼ãã¦æ¤ç´¢ããå¾ãæ¤ç´¢çµæãï¼ã¤ãã¤ã¯ãªãã¯ãã¦ä¿®æ£ãã¦ãã£ãã®ã§ãããåé¤ããæ¯ã«æ¤ç´¢çµæã®è©²å½è¡ã "INVALID" ã¨ãã赤æåã«å¤ãããã©ãã¾ã§ä½æ¥ããã®ããåããã®ã¯ã¡ãã£ã¨ä¾¿å©ã ãªã¨æãã¾ããã
src/test/java/ksbysample/common/test ã®ä¸ã® TestDataLoaderResource.java ã TestDataLoader.java ã«ãªãã¼ã ããå¾ããªã³ã¯å ã®å 容 ã«å¤æ´ãã¾ãã
src/test/java/ksbysample/common/test ã®ä¸ã® TestDataResource.java ã ãªã³ã¯å ã®å 容 ã«å¤æ´ãã¾ãã
clean ã¿ã¹ã¯å®è¡ â Rebuild Project å®è¡ â build ã¿ã¹ã¯ãå®è¡ãã¾ãã"BUILD SUCCESSFUL" ãåºåããããã¨ã確èªã§ãã¾ãã
Project View ãã Run 'All Tests' with Coverage ãå®è¡ãã¾ãããã¹ããå ¨ã¦æåãã¾ããã
ä¸æ¦ commit ãã¾ãã
ãã¼ã¸ã§ã³ãææ°ã§ãªãã©ã¤ãã©ãªãææ°ã«ãã
ãã¼ã¸ã§ã³çªå·ãæå®ãã¦ããã©ã¤ãã©ãªã®ææ°çã jCenter ( https://bintray.com/bintray/jcenter ) ã®ãµã¤ãã§ç¢ºèªãã¦ãææ°çã«æ´æ°ãã¾ããbuild.gradle ã ãªã³ã¯å ã®ãã®ï¼ã®å 容 ã«å¤æ´ãã¾ãã
Gradle projects View ã®å·¦ä¸ã«ãããRefresh all Gradle projectsããã¿ã³ãã¯ãªãã¯ãã¦æ´æ°ãã¾ãã
clean ã¿ã¹ã¯å®è¡ â Rebuild Project å®è¡ â build ã¿ã¹ã¯ãå®è¡ãã¾ãã"BUILD SUCCESSFUL" ãåºåããããã¨ã確èªã§ãã¾ãã
Project View ãã Run 'All Tests' with Coverage ãå®è¡ãã¾ããããã¡ãã¯ï¼ã¤ãã¹ãã失æãã¾ãããåå ã調æ»ãã¾ãã
ã¨ã©ã¼ã¡ãã»ã¼ã¸ã
java.lang.IllegalStateException: JMockit wasn't properly initialized; please ensure that jmockit precedes junit in the runtime classpath, or use @RunWith(JMockit.class)
ã¨åºåããã¦ãããããã¾ã§æ£å¸¸ã«åä½ãã¦ãã JMockit ã build.gradle æ´æ°å¾ã«åä½ããªããªã£ã¦ããã®ã§ãã©ã¤ãã©ãªã®é åºæå®ã§ JMockit ã JUnit ããåã«ãªã£ã¦ããªãã ãã ã¨æãã¾ããè¨å®ãä¿®æ£ãã¾ãã- IntelliJ IDEA ã®ã¡ã¤ã³ç»é¢ãããFileã-ãProject Structure...ããé¸æãã¾ãã
- ãProject Structureããã¤ã¢ãã°ã表示ããã¾ããç»é¢å·¦å´ã§ãProject Settingsã-ãModulesããé¸æãã¾ãã
- ç»é¢å·¦å´ã®ãªã¹ãã§ä¸ä¸ç¢å°ãã¿ã³ãæ¼ã㦠jmockit ã junit ã®ä¸ã«ç§»åãã¾ãã
- ãOKããã¿ã³ãã¯ãªãã¯ãã¦ãã¤ã¢ãã°ãéãã¾ãã
å度 Project View ãã Run 'All Tests' with Coverage ãå®è¡ãã¾ããä»åº¦ã¯ãã¹ããå ¨ã¦æåãã¾ããã
ä¸æ¦ commit ãã¾ãã
PushãPull Requestããã¼ã¸
- GitHub 㸠Pushãfeature/51-issue -> 1.0.x 㸠Pull Requestã1.0.x ã§ãã¼ã¸ãfeature/51-issue ãã©ã³ããåé¤ãããã¾ãã
次åã¯ããã
貸åºæ¿èªç»é¢ ( æ¿èªè ã®ã¿ ) ã®ä½æã«é²ã¿ã¾ãã
ã½ã¼ã¹ã³ã¼ã
build.gradle
â ãã®ï¼
buildscript { ext { springBootVersion = '1.2.8.RELEASE' } repositories { jcenter() maven { url "http://repo.spring.io/repo/" } } dependencies { classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}") classpath("io.spring.gradle:dependency-management-plugin:0.5.3.RELEASE") // for Grgit classpath("org.ajoberstar:grgit:1.4.1") // Gradle Download Task classpath("de.undercouch:gradle-download-task:2.0.0") } } apply plugin: 'java' apply plugin: 'eclipse' apply plugin: 'idea' apply plugin: 'spring-boot' apply plugin: 'io.spring.dependency-management' apply plugin: 'de.undercouch.download' apply plugin: 'groovy' sourceCompatibility = 1.8 targetCompatibility = 1.8 compileJava.options.compilerArgs = ['-Xlint:all'] compileTestGroovy.options.compilerArgs = ['-Xlint:all'] compileTestJava.options.compilerArgs = ['-Xlint:all'] // for Doma 2 // Javaã¯ã©ã¹ã¨SQLãã¡ã¤ã«ã®åºåå ãã£ã¬ã¯ããªãåãã«ãã processResources.destinationDir = compileJava.destinationDir // ã³ã³ãã¤ã«ããåã«SQLãã¡ã¤ã«ãåºåå ãã£ã¬ã¯ããªã«ã³ãã¼ããããã«ä¾åé¢ä¿ãé転ãã compileJava.dependsOn processResources jar { baseName = 'ksbysample-webapp-lending' version = '1.0.0-SNAPSHOT' } eclipse { classpath { containers.remove('org.eclipse.jdt.launching.JRE_CONTAINER') containers 'org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8' } } idea { module { inheritOutputDirs = false outputDir = file("$buildDir/classes/main/") } } configurations { domaGenRuntime } repositories { jcenter() } dependencies { def jdbcDriver = "org.postgresql:postgresql:9.4-1204-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.thymeleaf.extras:thymeleaf-extras-springsecurity3") 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.springframework.boot:spring-boot-starter-security") compile("org.springframework.boot:spring-boot-starter-redis") compile("org.springframework.boot:spring-boot-starter-amqp") compile("org.codehaus.janino:janino") compile("com.fasterxml.jackson.datatype:jackson-datatype-jsr310") compile("com.fasterxml.jackson.dataformat:jackson-dataformat-xml") testCompile("org.springframework.boot:spring-boot-starter-test") // (ãããã) gradle ã§ãã¹ããå®è¡ããå ´åã« spring-security-test-4.0.3.RELEASE.jar ãã classpath ã«æå®ããã // ãã¹ãã失æããããã3.2.9.RELEASE ãæè¨ãã¦ãã testCompile("org.springframework.security:spring-security-core:3.2.9.RELEASE") testCompile("org.springframework.security:spring-security-web:3.2.9.RELEASE") // (ããã¾ã§) ------------------------------------------------------------------------------------------------------ testCompile("org.springframework.security:spring-security-test:4.0.3.RELEASE") testCompile("org.yaml:snakeyaml") testCompile("org.codehaus.groovy:groovy-all") testCompile("org.spockframework:spock-core") testCompile("org.spockframework:spock-spring") // spring-boot-gradle-plugin ã«ãããã¼ã¸ã§ã³çªå·ãèªåã§è¨å®ãããªããã® compile("${jdbcDriver}") compile("org.seasar.doma:doma:2.5.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.6") compile("com.google.guava:guava:18.0") compile("org.springframework.session:spring-session:1.0.1.RELEASE") compile("org.simpleframework:simple-xml:2.7.1") compile("com.univocity:univocity-parsers:1.5.6") compile("org.thymeleaf.extras:thymeleaf-extras-java8time:2.1.0.RELEASE") testCompile("org.dbunit:dbunit:2.5.1") testCompile("com.icegreen:greenmail:1.4.1") testCompile("org.assertj:assertj-core:3.2.0") testCompile("com.jayway.jsonpath:json-path:2.0.0") testCompile("org.jmockit:jmockit:1.19") // for Doma-Gen domaGenRuntime("org.seasar.doma:doma-gen:2.5.0") domaGenRuntime("${jdbcDriver}") }
springBootVersion = '1.2.7.RELEASE'
âspringBootVersion = '1.2.8.RELEASE'
ã«å¤æ´ãã¾ãã- dependencies ã¿ã¹ã¯ã®ä»¥ä¸ã®ç¹ãå¤æ´ãã¾ãã
testCompile("org.springframework.security:spring-security-core:3.2.8.RELEASE")
âtestCompile("org.springframework.security:spring-security-core:3.2.9.RELEASE")
ã«å¤æ´ãã¾ããtestCompile("org.springframework.security:spring-security-web:3.2.8.RELEASE")
âtestCompile("org.springframework.security:spring-security-web:3.2.9.RELEASE")
ã«å¤æ´ãã¾ããtestCompile("org.springframework.security:spring-security-test:4.0.2.RELEASE")
㯠4.0.3.RELEASE ã«ã¯å¤æ´ãã¾ããã å¤æ´ããã¨ã¨ã©ã¼ãåºã¦ãã¹ãã失æããããã§ãã
â ãã®ï¼
repositories { jcenter() } dependencyManagement { imports { mavenBom 'io.spring.platform:platform-bom:1.1.5.RELEASE' } } dependencies { def jdbcDriver = "org.postgresql:postgresql:9.4-1204-jdbc41"
- repositories ã®ä¸ã«
dependencyManagement { ... }
ã追å ãã¾ããSpring Boot 1.2.8 ã«å¯¾å¿ããio.spring.platform:platform-bom
ã®ãã¼ã¸ã§ã³ã¯ 1.1.5 ãªã®ã§ 1.1.5.RELEASE ãæå®ãã¾ãã
â ãã®ï¼
dependencies { def jdbcDriver = "org.postgresql:postgresql:9.4-1204-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.thymeleaf.extras:thymeleaf-extras-springsecurity3") 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.springframework.boot:spring-boot-starter-security") compile("org.springframework.boot:spring-boot-starter-redis") compile("org.springframework.boot:spring-boot-starter-amqp") compile("org.codehaus.janino:janino") compile("com.fasterxml.jackson.datatype:jackson-datatype-jsr310") compile("com.fasterxml.jackson.dataformat:jackson-dataformat-xml") testCompile("org.springframework.boot:spring-boot-starter-test") // (ãããã) gradle ã§ãã¹ããå®è¡ããå ´åã« spring-security-test-4.0.3.RELEASE.jar ãã classpath ã«æå®ããã // ãã¹ãã失æããããã3.2.9.RELEASE ãæè¨ãã¦ãã testCompile("org.springframework.security:spring-security-core:3.2.9.RELEASE") testCompile("org.springframework.security:spring-security-web:3.2.9.RELEASE") // (ããã¾ã§) ------------------------------------------------------------------------------------------------------ testCompile("org.springframework.security:spring-security-test:4.0.2.RELEASE") testCompile("org.yaml:snakeyaml") testCompile("org.codehaus.groovy:groovy-all") testCompile("org.spockframework:spock-core") testCompile("org.spockframework:spock-spring") // spring-boot-gradle-plugin ã«ãããã¼ã¸ã§ã³çªå·ãèªåã§è¨å®ãããªããã® compile("${jdbcDriver}") compile("org.seasar.doma:doma:2.6.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.6") compile("com.google.guava:guava:19.0") compile("org.springframework.session:spring-session:1.0.1.RELEASE") compile("org.simpleframework:simple-xml:2.7.1") compile("com.univocity:univocity-parsers:1.5.6") compile("org.thymeleaf.extras:thymeleaf-extras-java8time:2.1.0.RELEASE") testCompile("org.dbunit:dbunit:2.5.1") testCompile("com.icegreen:greenmail:1.4.1") testCompile("org.assertj:assertj-core:3.2.0") testCompile("com.jayway.jsonpath:json-path:2.1.0") testCompile("org.jmockit:jmockit:1.21") // for Doma-Gen domaGenRuntime("org.seasar.doma:doma-gen:2.6.0") domaGenRuntime("${jdbcDriver}") }
- dependencies ã¿ã¹ã¯ã®ä»¥ä¸ã®ã©ã¤ãã©ãªã®ãã¼ã¸ã§ã³ãå¤æ´ãã¾ãã
compile("org.seasar.doma:doma:2.5.0")
âcompile("org.seasar.doma:doma:2.6.0")
ã¸å¤æ´ãã¾ããcompile("com.google.guava:guava:18.0")
âcompile("com.google.guava:guava:19.0")
ã¸å¤æ´ãã¾ããtestCompile("com.jayway.jsonpath:json-path:2.0.0")
âtestCompile("com.jayway.jsonpath:json-path:2.1.0")
ã¸å¤æ´ãã¾ ããtestCompile("org.jmockit:jmockit:1.19")
âtestCompile("org.jmockit:jmockit:1.21")
ã¸å¤æ´ãã¾ããdomaGenRuntime("org.seasar.doma:doma-gen:2.5.0")
âdomaGenRuntime("org.seasar.doma:doma-gen:2.6.0")
ã¸å¤æ´ãã¾ãã
CustomModelResultMatchers.java
package ksbysample.common.test; import org.hamcrest.Matcher; import org.springframework.expression.EvaluationContext; import org.springframework.expression.Expression; import org.springframework.expression.ExpressionParser; import org.springframework.expression.spel.standard.SpelExpressionParser; import org.springframework.expression.spel.support.StandardEvaluationContext; import org.springframework.test.web.servlet.ResultMatcher; import org.springframework.test.web.servlet.result.ModelResultMatchers; import org.springframework.ui.ModelMap; import java.util.regex.Pattern; import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.is; import static org.hamcrest.Matchers.notNullValue; public class CustomModelResultMatchers extends ModelResultMatchers { public static CustomModelResultMatchers modelEx() { return new CustomModelResultMatchers(); } @SuppressWarnings("unchecked") public <T> ResultMatcher property(final String nameAndProperty, final Matcher<T> matcher) { return mvcResult -> { // <ã¤ã³ã¹ã¿ã³ã¹å>.<ããããã£å> ( ä¾: page.number ) ã®å½¢å¼ã®æååã // ã¤ã³ã¹ã¿ã³ã¹åã¨ããããã£åã«åå²ãã Pattern p = Pattern.compile("^(\\S+?)\\.(\\S+)$"); java.util.regex.Matcher m = p.matcher(nameAndProperty); assertThat(m.find(), is(true)); String name = m.group(1); String property = m.group(2); // ããããã£ã®å¤ãåå¾ãã¦ãã§ãã¯ãã ModelMap modelMap = mvcResult.getModelAndView().getModelMap(); Object object = modelMap.get(name); assertThat(object, is(notNullValue())); EvaluationContext context = new StandardEvaluationContext(object); ExpressionParser parser = new SpelExpressionParser(); Expression exp = parser.parseExpression(property); Object value = exp.getValue(context); assertThat((T) value, matcher); }; } }
import static org.springframework.test.util.MatcherAssertionErrors.assertThat;
âimport static org.hamcrest.MatcherAssert.assertThat;
ã«å¤æ´ãã¾ãããªã hamcrest ã® assertThat ã使ã£ã¦ãªãã£ãã®ã ããã¨ãããªãä»é ã«ãªã£ã¦è¦åãåºãããã«ãªã£ãã®ã ããã¨ããããããçåã§ãããã
TestDataLoader.java
package ksbysample.common.test; import org.dbunit.database.DatabaseConnection; import org.dbunit.database.IDatabaseConnection; import org.dbunit.dataset.IDataSet; import org.dbunit.dataset.ReplacementDataSet; import org.dbunit.dataset.csv.CsvDataSet; import org.dbunit.operation.DatabaseOperation; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import javax.sql.DataSource; import java.io.File; @Component public class TestDataLoader { @Autowired private DataSource dataSource; public void load(String csvDir) { try { IDatabaseConnection conn = null; try { conn = new DatabaseConnection(dataSource.getConnection()); IDataSet dataSet = new CsvDataSet(new File(csvDir)); ReplacementDataSet replacementDataset = new ReplacementDataSet(dataSet); replacementDataset.addReplacementObject("[null]", null); DatabaseOperation.CLEAN_INSERT.execute(conn, replacementDataset); } finally { if (conn != null) conn.close(); } } catch (Exception e) { e.printStackTrace(); } } }
TestDataResource.java
package ksbysample.common.test; import org.dbunit.database.DatabaseConnection; import org.dbunit.database.IDatabaseConnection; import org.dbunit.database.QueryDataSet; import org.dbunit.dataset.DefaultDataSet; import org.dbunit.dataset.DefaultTable; import org.dbunit.dataset.IDataSet; import org.dbunit.dataset.ReplacementDataSet; import org.dbunit.dataset.csv.CsvDataSet; import org.dbunit.dataset.xml.FlatXmlDataSet; import org.dbunit.dataset.xml.FlatXmlDataSetBuilder; import org.dbunit.operation.DatabaseOperation; import org.junit.rules.ExternalResource; import org.junit.rules.TestWatcher; import org.junit.runner.Description; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import javax.sql.DataSource; import java.io.File; import java.io.FileOutputStream; import java.lang.annotation.Annotation; import java.nio.file.Files; import java.util.Arrays; import java.util.Collection; import java.util.List; @Component public class TestDataResource extends TestWatcher { private final String TESTDATA_DIR = "src/test/resources/testdata/base"; private final String BACKUP_FILE_NAME = "ksbylending_backup"; private final List<String> BACKUP_TABLES = Arrays.asList( "user_info" , "user_role" , "library_forsearch" , "lending_app" , "lending_book" ); @Autowired private DataSource dataSource; @Autowired private TestDataLoader testDataLoader; private File backupFile; @Override protected void starting(Description description) { IDatabaseConnection conn = null; try { try { conn = new DatabaseConnection(dataSource.getConnection()); // ããã¯ã¢ãããåå¾ãã QueryDataSet partialDataSet = new QueryDataSet(conn); for (String backupTable : BACKUP_TABLES) { partialDataSet.addTable(backupTable); } ReplacementDataSet replacementDatasetBackup = new ReplacementDataSet(partialDataSet); replacementDatasetBackup.addReplacementObject("", "[null]"); backupFile = File.createTempFile(BACKUP_FILE_NAME, "xml"); try (FileOutputStream fos = new FileOutputStream(backupFile)) { FlatXmlDataSet.write(replacementDatasetBackup, fos); } // ãã¹ããã¼ã¿ã«å ¥ãæ¿ãã IDataSet dataSet = new CsvDataSet(new File(TESTDATA_DIR)); ReplacementDataSet replacementDataset = new ReplacementDataSet(dataSet); replacementDataset.addReplacementObject("[null]", null); DatabaseOperation.CLEAN_INSERT.execute(conn, replacementDataset); // ãã¹ãã¡ã½ããã« @TestData ã¢ããã¼ã·ã§ã³ãä»å ããã¦ããå ´åã«ã¯ã // ã¢ããã¼ã·ã§ã³ã§æå®ããããã¹ããã¼ã¿ããã¼ããã Collection<Annotation> annotationList = description.getAnnotations(); for (Annotation annotation : annotationList) { if (annotation instanceof TestData) { TestData testData = (TestData)annotation; testDataLoader.load(testData.value()); } } } finally { if (conn != null) conn.close(); } } catch (Exception e) { throw new RuntimeException(e); } } @Override protected void finished(Description description) { try { IDatabaseConnection conn = null; try { conn = new DatabaseConnection(dataSource.getConnection()); // ããã¯ã¢ãããããªã¹ãã¢ãã if (backupFile != null) { IDataSet dataSet = new FlatXmlDataSetBuilder().build(backupFile); ReplacementDataSet replacementDatasetRestore = new ReplacementDataSet(dataSet); replacementDatasetRestore.addReplacementObject("[null]", null); DatabaseOperation.CLEAN_INSERT.execute(conn, replacementDatasetRestore); } } finally { if (backupFile != null) { Files.delete(backupFile.toPath()); backupFile = null; } try { if (conn != null) conn.close(); } catch (Exception ignored) { } } } catch (Exception e) { throw new RuntimeException(e); } } }
extends ExternalResource
âextends TestWatcher
ã¸å¤æ´ãã¾ãã@Autowired private TestDataLoader testDataLoader;
ã追å ãã¾ããprotected void before() throws Exception {
âprotected void starting(Description description) {
ã¸å¤æ´ãã¾ãã- starting ã¡ã½ããã« @TestData ã¢ããã¼ã·ã§ã³ã§æå®ããããã¹ããã¼ã¿ããã¼ãããå¦çã追å ãã¾ãã
- starting ã¡ã½ããå
ã®å¦çã
try { ... } catch (Exception e) { throw new RuntimeException(e); }
ã§å²ã¿ã¾ãã protected void after() {
âprotected void finished(Description description) {
ã¸å¤æ´ãã¾ãã- finished ã¡ã½ããå
ã®
e.printStackTrace();
âthrow new RuntimeException(e);
ã¸å¤æ´ãã¾ãã
å±¥æ´
2016/01/06
åççºè¡ã