Spring Boot ã§æ¸ç±ã®è²¸åºç¶æ³ç¢ºèªã»è²¸åºç³è«ãã Web ã¢ããªã±ã¼ã·ã§ã³ãä½ã ( ãã®ï¼ï¼ )( 貸åºç¶æ³åå¾ã¿ã¹ã¯ã®ä½æï¼ )
æ¦è¦
Spring Boot ã§æ¸ç±ã®è²¸åºç¶æ³ç¢ºèªã»è²¸åºç³è«ãã Web ã¢ããªã±ã¼ã·ã§ã³ãä½ã ( ãã®ï¼ï¼ )( 貸åºç¶æ³åå¾ã¿ã¹ã¯ã®ä½æï¼ ) ã®ç¶ãã§ãã
- ä»åã®æé ã§ç¢ºèªã§ããã®ã¯ä»¥ä¸ã®å
容ã§ãã
- 貸åºç¶æ³åå¾ã¿ã¹ã¯ã®ä½æ
- ãã¹ãã®ä½æ
- 貸åºç¶æ³åå¾ã¿ã¹ã¯ã®ä½æ
åç §ãããµã¤ãã»æ¸ç±
-
com.google.common.io - Class Files
http://docs.guava-libraries.googlecode.com/git/javadoc/com/google/common/io/Files.html- Mail001Helper ã¯ã©ã¹ã®ãã¹ããæ¸ãéã«ãæ¯è¼å¯¾è±¡ã®ã¡ã¼ã«æ¬æãè¨è¿°ãããã¡ã¤ã«ãèªã¿è¾¼ã㧠String åã®å¤æ°ã«ã»ããããããã« Files ã¯ã©ã¹ã® toString ã¡ã½ãããå©ç¨ãã¾ããã
-
Read file line by line in java 8: Streams of Lines Example
http://howtodoinjava.com/2014/05/04/read-file-line-by-line-in-java-8-streams-of-lines-example/- Guava ã® Files.toString ã¡ã½ããã§ã¯ãªã java.nio.File ããã±ã¼ã¸ã® Files.lines ã¡ã½ãããããã使ããªããã¨æãã調æ»ããæã«åç §ãã¾ããã
- Files.lines ããã®ã¾ã¾åã« Stream API ã§å¦çããã®ã§ã¯ãªããä¸æ¦ try-with-resources æ§æã§å²ã£ã¦ããã®ãå°è±¡ã«æ®ã£ãã®ã§ã¡ã¢ã¨ãã¦æ®ãã¦ããã¾ãããªã½ã¼ã¹ã®ã¯ãã¼ãºå¿ãã«æ³¨æããªãã¨ãããªãã¨ã¯ç¥ãã¾ããã§ããããã
ç®æ¬¡
- ãã¹ãä½æ対象ã®ã¯ã©ã¹ã決ãã
- calilapi ã®ã¬ã¹ãã³ã¹å±éç¨ã®ã¯ã©ã¹ã reponse ããã±ã¼ã¸ã®ä¸ã«ç§»åãã
- EmailService.java ã®ãã¹ãã¯ã©ã¹ã ksbysample-webapp-email ããæã£ã¦ãã
- Mail001Helper ã¯ã©ã¹ã®ãã¹ãã®ä½æ
- InquiringStatusOfBookQueueListener ã¯ã©ã¹ã®ãã¹ãã®ä½æ
- LendingUserDetailsHelper ã¯ã©ã¹ã®ãã¹ãã®ä½æ
- UploadBooklistFormValidator ã¯ã©ã¹ã®ãã¹ãã®ä½æ
- å ¨ã¦ã®ãã¹ããæåããã確èªãã
- commitãPushãPull Requestããã¼ã¸
- 次åã¯ããã
- ã¡ã¢æ¸ã
æé
ãã¹ãä½æ対象ã®ã¯ã©ã¹ã決ãã
Gradle projects View ãã printClassWhatNotMakeTest ã¿ã¹ã¯ãå®è¡ãã¾ãã
åºåãããã¯ã©ã¹ã«å¯¾ãã¦ä»¥ä¸ã®å¯¾å¿ãè¡ãã¾ãã
src/main/java/ksbysample/webapp/lending/service/calilapi ã®ä¸ã«ä½æããã¬ã¹ãã³ã¹å±éç¨ã®ã¯ã©ã¹ã¯ reponse ããã±ã¼ã¸ãä½æãã¦ãã®ä¸ã«ç§»åããsrc/main/java/ksbysample/webapp/lending/service/calilapi/response ã printClassWhatNotMakeTest ã¿ã¹ã¯ã®ãã§ãã¯å¯¾è±¡å¤ã«ãã¾ãã
src/main/java/ksbysample/webapp/lending/service/EmailService.java ã¯ãã¹ãã¯ã©ã¹ã ksbysample-webapp-email ããæã£ã¦ãã¾ãã
以ä¸ã®ã¯ã©ã¹ã¯ãã¹ããä½æãã¾ãã
- src/main/java/ksbysample/webapp/lending/helper/mail/Mail001Helper.java
- src/main/java/ksbysample/webapp/lending/listener/rabbitmq/InquiringStatusOfBookQueueListener.java
- src/main/java/ksbysample/webapp/lending/security/LendingUserDetailsHelper.java
- src/main/java/ksbysample/webapp/lending/web/booklist/UploadBooklistFormValidator.java
calilapi ã®ã¬ã¹ãã³ã¹å±éç¨ã®ã¯ã©ã¹ã reponse ããã±ã¼ã¸ã®ä¸ã«ç§»åãã
src/main/java/ksbysample/webapp/lending/service/calilapi ã®ä¸ã« response ããã±ã¼ã¸ãä½æãã¾ãã
src/main/java/ksbysample/webapp/lending/service/calilapi ã®ä¸ã®ä»¥ä¸ã®ã¯ã©ã¹ã reponse ããã±ã¼ã¸ã®ä¸ã¸ç§»åãã¾ãã
src/main/java/ksbysample/webapp/lending/service/calilapi/response ã printClassWhatNotMakeTest ã¿ã¹ã¯ã®ãã§ãã¯å¯¾è±¡å¤ã«ãã¾ããbuild.gradle ã ãªã³ã¯å ã®å 容 ã«å¤æ´ãã¾ãã
EmailService.java ã®ãã¹ãã¯ã©ã¹ã ksbysample-webapp-email ããæã£ã¦ãã
https://github.com/ksby/ksbysample-webapp-email/tree/1.0.x/src/test/java/ksbysample/webapp/email/service ã®ä¸ã«ãã EmailServiceTest.java ãæã£ã¦ãã¦ãsrc/test/java/ksbysample/webapp/lending/service ã®ä¸ã«é ç½®ãã¾ããé ç½®å¾
public static class MailServerResourceãå©ç¨ãã¦ã¡ã¼ã«éä¿¡ããå ´å{ ... }
以å¤ã®ãã¹ãã¯ã©ã¹ãåé¤ãããªã³ã¯å ã®å 容 ã«å¤æ´ãã¾ãããã¹ããå®è¡ãã¾ããEmailServiceTest ã¯ã©ã¹ã®ã¯ã©ã¹åã«ã«ã¼ã½ã«ã移åããã³ã³ããã¹ãã¡ãã¥ã¼ã表示å¾ãRun 'EmailServiceTest' with Coverageããé¸æãã¾ãã
ãã¹ããæåãããã¨ã確èªã§ãã¾ãã
Mail001Helper ã¯ã©ã¹ã®ãã¹ãã®ä½æ
src/main/java/ksbysample/webapp/lending/helper/mail ã®ä¸ã® Mail001Helper.java ã§ãCreate Testããã¤ã¢ãã°ã表示ãããã¹ãã¯ã©ã¹ãä½æãã¾ãã
src/test/java/ksbysample/webapp/lending/helper/mail ã®ä¸ã« Mail001HelperTest.java ãä½æããã¾ãã
src/test/resources/ksbysample/webapp/lending ã®ä¸ã« helper/mail/assertdata/001 ãã£ã¬ã¯ããªãä½æãã¾ãã
src/test/resources/ksbysample/webapp/lending/helper/mail/assertdata/001 ã®ä¸ã« message.txt ãä½æãã¾ããä½æå¾ããªã³ã¯å ã®å 容 ã«å¤æ´ãã¾ãã
src/test/java/ksbysample/webapp/lending/helper/mail ã®ä¸ã® Mail001HelperTest.java ã ãªã³ã¯å ã®å 容 ã«å¤æ´ãã¾ãã
ãã¹ããå®è¡ãã¾ããMail001HelperTest ã¯ã©ã¹ã®ã¯ã©ã¹åã«ã«ã¼ã½ã«ã移åããã³ã³ããã¹ãã¡ãã¥ã¼ã表示å¾ãRun 'Mail001HelperTest' with Coverageããé¸æãã¾ãã
ãã¹ããæåãããã¨ã確èªã§ãã¾ãã
InquiringStatusOfBookQueueListener ã¯ã©ã¹ã®ãã¹ãã®ä½æ
src/main/java/ksbysample/webapp/lending/listener/rabbitmq ã®ä¸ã® InquiringStatusOfBookQueueListener.java ã§ãCreate Testããã¤ã¢ãã°ã表示ãããã¹ãã¯ã©ã¹ãä½æãã¾ãã
src/test/java/ksbysample/webapp/lending/listener/rabbitmq ã®ä¸ã« InquiringStatusOfBookQueueListenerTest.java ãä½æããã¾ãã
DB ã«ã»ãããããã¹ããã¼ã¿ãå®ç¾©ãã¾ããsrc/test/resources/ksbysample/webapp/lending ã®ä¸ã« listener/rabbitmq/testdata/001 ãã£ã¬ã¯ããªãä½æãã¾ãã
src/test/resources/ksbysample/webapp/lending/helper/mail/testdata/001 ã®ä¸ã« table-ordering.txt, lending_app.csv, lending_book.csv ãä½æãã¾ããä½æå¾ããªã³ã¯å ã®å 容 ã«å¤æ´ãã¾ãã
æ¤è¨¼ç¨ãã¼ã¿ãå®ç¾©ãã¾ããsrc/test/resources/ksbysample/webapp/lending/listener/rabbitmq ã®ä¸ã« assertdata/001 ãã£ã¬ã¯ããªãä½æãã¾ãã
src/test/resources/ksbysample/webapp/lending/listener/rabbitmq/assertdata/001 ã®ä¸ã« table-ordering.txt, lending_app.csv, lending_book.csv ãä½æãã¾ããä½æå¾ããªã³ã¯å ã®å 容 ã«å¤æ´ãã¾ãã
ãã¹ãã§ã¯ receiveMessage ã¡ã½ããå ã® calilApiService.check ãã¢ãã¯ã«å ¥ãæ¿ãã¦ã«ã¼ãªã«ã®èµæ¸æ¤ç´¢ WebAPI ãå¼ã³åºããªãããã«ãã¾ããã¢ãã¯ã§ Book ã¯ã©ã¹åã³é¢é£ã¯ã©ã¹ ( SystemData ã¯ã©ã¹ãLibkey ã¯ã©ã¹ ) ã®ã¤ã³ã¹ã¿ã³ã¹ãçæããããããããã«ããã®ï¼ã¤ã®ã¯ã©ã¹ã« lombok ã® @AllArgsConstructor ã¢ããã¼ã·ã§ã³ãä»å ãã¾ã ( @NoArgsConstructor ããªãã¨å®è¡æã«ã¨ã©ã¼ãåºãããä¸ç·ã«ä»å ãã¾ã )ãsrc/main/java/ksbysample/webapp/lending/listener/rabbitmq/response ã®ä¸ã® Book.java, SystemData.java, Libkey.java ã ãªã³ã¯å ã®å 容 ã«å¤æ´ãã¾ãã
src/test/java/ksbysample/webapp/lending/listener/rabbitmq ã®ä¸ã® InquiringStatusOfBookQueueListenerTest.java ã ãªã³ã¯å ã®å 容 ã«å¤æ´ãã¾ãã
ãã¹ããå®è¡ãã¾ããInquiringStatusOfBookQueueListenerTest ã¯ã©ã¹ã®ã¯ã©ã¹åã«ã«ã¼ã½ã«ã移åããã³ã³ããã¹ãã¡ãã¥ã¼ã表示å¾ãRun 'InquiringStatusOfBookQueueList...' with Coverageããé¸æãã¾ãã
ãã¹ããæåãããã¨ã確èªã§ãã¾ãã
LendingUserDetailsHelper ã¯ã©ã¹ã®ãã¹ãã®ä½æ
src/main/java/ksbysample/webapp/lending/security ã®ä¸ã® LendingUserDetailsHelper.java ã§ãCreate Testããã¤ã¢ãã°ã表示ãããã¹ãã¯ã©ã¹ãä½æãã¾ãã
src/test/java/ksbysample/webapp/lending/security ã®ä¸ã« LendingUserDetailsHelperTest.java ãä½æããã¾ãã
src/test/java/ksbysample/webapp/lending/security ã®ä¸ã® LendingUserDetailsHelperTest.java ã ãªã³ã¯å ã®å 容 ã«å¤æ´ãã¾ãã
ãã¹ããå®è¡ãã¾ããLendingUserDetailsHelperTest ã¯ã©ã¹ã®ã¯ã©ã¹åã«ã«ã¼ã½ã«ã移åããã³ã³ããã¹ãã¡ãã¥ã¼ã表示å¾ãRun 'LendingUserDetailsHe...' with Coverageããé¸æãã¾ãã
ãã¹ããæåãããã¨ã確èªã§ãã¾ãã
UploadBooklistFormValidator ã¯ã©ã¹ã®ãã¹ãã®ä½æ
src/main/java/ksbysample/webapp/lending/web/booklist ã®ä¸ã® UploadBooklistFormValidator.java ã§ãCreate Testããã¤ã¢ãã°ã表示ãããã¹ãã¯ã©ã¹ãä½æãã¾ãã
src/test/java/ksbysample/webapp/lending/web/booklist ã®ä¸ã« UploadBooklistFormValidatorTest.java ãä½æããã¾ãã
src/test/java/ksbysample/webapp/web/booklist ã®ä¸ã® UploadBooklistFormValidatorTest.java ã ãªã³ã¯å ã®å 容 ã«å¤æ´ãã¾ãã
ãã¹ããå®è¡ãã¾ããUploadBooklistFormValidatorTest ã¯ã©ã¹ã®ã¯ã©ã¹åã«ã«ã¼ã½ã«ã移åããã³ã³ããã¹ãã¡ãã¥ã¼ã表示å¾ãRun 'UploadBooklistFormVa...' with Coverageããé¸æãã¾ãã
ãã¹ããæåãããã¨ã確èªã§ãã¾ãã
å ¨ã¦ã®ãã¹ããæåããã確èªãã
æå¾ã«å ¨ã¦ã®ãã¹ããæåããã確èªãã¾ããProject View ã®ã«ã¼ãã§ã³ã³ããã¹ãã¡ãã¥ã¼ã表示ãã¦ãRun 'Tests in 'ksbysample...' with Coverageããé¸æãã¾ãã
ãã¹ããå®è¡ãããå ¨ã¦æåãããã¨ã確èªã§ãã¾ãã
clean ã¿ã¹ã¯ã®å®è¡âãRebuild Projectãã¡ãã¥ã¼ã®å®è¡âbuild ã¿ã¹ã¯ã®å®è¡ãè¡ãã"BUILD SUCCESSFUL" ã®ã¡ãã»ã¼ã¸ãåºåããããã¨ã確èªãã¾ãã
commitãPushãPull Requestããã¼ã¸
ããã¾ã§ã®å¤æ´å 容ã commit ãã¾ãã
ã³ãã³ãã©ã¤ã³ãã以ä¸ã®ã³ãã³ããå®è¡ã㦠commit ãï¼ã¤ã«ã¾ã¨ãã¾ãã
> git rebase -i HEAD~3
> git commit --amend -m "#37 貸åºç¶æ³åå¾ã¿ã¹ã¯ãä½æãã¾ããã"GitHub 㸠Pushãfeature/37-issue -> 1.0.x 㸠Pull Requestã1.0.x ã§ãã¼ã¸ãfeature/37-issue ãã©ã³ããåé¤ãããã¾ãã
次åã¯ããã
çªå¤ç·¨ã¨ãã¦ãã¹ãã¯ã©ã¹å 㧠@Autowired 㧠DI ããã¯ã©ã¹ã®ä¸ã® @Autowired 㧠DI ãã¦ãããã£ã¼ã«ããä¸é¨ã ã JMockit ã§ã¢ãã¯åããæ¹æ³ãã¾ã¨ãã¾ã ( ä»åããããããã¨ãã¦ããªã試è¡é¯èª¤ãã¾ããããã )ã
ãã®å¾ã§ IntelliJ IDEA Ultimate Edition ã 15.0 ã¸ãã¼ã¸ã§ã³ã¢ãããã¦ããã 次ã®è²¸åºç³è«ç»é¢ã®ä½æã«é²ã¿ã¾ãã
ã¡ã¢æ¸ã
IntelliJ IDEA Ulitimate Edition ã® Database tools ã§åºåãã CSV ãã¡ã¤ã«ã«ã¯ NULL 㯠"NULL" ã¨åºåããããã¨ã«ä»åãã¹ããã¼ã¿ãä½æãã¦ãã¦æ°ä»ãã¾ãããTestDataResource ã TestDataLoaderResource 㧠NULL ã«å¤æããæååã "[null]" ã«ãã¦ãã¾ããã"NULL" ã«ããæ¹ãããããã§ãããã ã ksbysample-webapp-lending ã§ã¯ãã®ã¾ã¾é²ãã¾ãã
InquiringStatusOfBookQueueListenerTest ã®ãã¹ããä½æãã¦ããæã«æ°ä»ãã¾ãããèªä½ãããã¹ãæ¤è¨¼ç¨ã¯ã©ã¹ TableDataAssert ã® assertEquals ã¡ã½ããã®ï¼çªç®ã®å¼æ°ã«ã¯æ¤è¨¼å¯¾è±¡å¤ã®ã«ã©ã ãåæããããå®è£ ãã¦ããã®ã§ãããä»åã®ãã¹ãã®ããã«ï¼ã«ã©ã ã ãæ¤è¨¼ãããå ´åã«ã¯æ¤è¨¼å¯¾è±¡å¤ã®ã«ã©ã ãåæããã®ã¯ã¡ãã£ã¨æéã§ãæ¤è¨¼ãããã«ã©ã ã ãæå®ã§ããããã«ã§ããªããã¨æã£ã¦å度è¦ç´ããã¨ãã DefaultColumnFilter ã¯ã©ã¹ã« excludedColumnsTable ã¡ã½ãã以å¤ã« includedColumnsTable ã¡ã½ãããåå¨ãã¦ãã¾ãããè¿ããã¡ã« TableDataAssert ã¯ã©ã¹ãä¿®æ£ãã¦ãæ¤è¨¼å¯¾è±¡ã®ã«ã©ã ã ãæå®ã§ããã¡ã½ãããç¨æãããã¨æãã¾ãã
ãã¹ããã¼ã¿ã®ä½æã»é ç½®æ¹æ³ãããã¹ãã§ä½¿ç¨ããå ±éã¯ã©ã¹ã®ä½ææ¹æ³ã®ã«ã¼ã«ã決ãã¦ããããã¨æãã¾ããããã¹ããã¼ã¿ãä½æããå¿ è¦ããããã©ã©ãã«ä½ãããã¨ãããããã¹ããæ¸ãã¦ããã¨å¥ã®ãã¹ãã§ä½æããã¡ã½ãããå©ç¨ãããã¨ããä»åç¹ã«å¼·ãæãã¾ããããã¡ããè¿ããã¡ã«ï¼åº¦èãã¦ã¿ããã¨æãã¾ãã
ã½ã¼ã¹ã³ã¼ã
build.gradle
task printClassWhatNotMakeTest << { def srcDir = new File("src/main/java"); def excludePaths = [ "src/main/java/ksbysample/webapp/lending/Application.java" , "src/main/java/ksbysample/webapp/lending/config" , "src/main/java/ksbysample/webapp/lending/dao" , "src/main/java/ksbysample/webapp/lending/entity" , "src/main/java/ksbysample/webapp/lending/exception" , "src/main/java/ksbysample/webapp/lending/helper/page/PagenationHelper.java" , "src/main/java/ksbysample/webapp/lending/security/LendingUser.java" , "src/main/java/ksbysample/webapp/lending/security/RoleAwareAuthenticationSuccessHandler.java" , "src/main/java/ksbysample/webapp/lending/service/calilapi/response" , "src/main/java/ksbysample/webapp/lending/service/file/BooklistCSVRecord.java" , "src/main/java/ksbysample/webapp/lending/service/openweathermapapi" , "src/main/java/ksbysample/webapp/lending/service/queue/InquiringStatusOfBookQueueMessage.java" , "src/main/java/ksbysample/webapp/lending/util/doma" , "src/main/java/ksbysample/webapp/lending/util/velocity/VelocityUtils.java" , "src/main/java/ksbysample/webapp/lending/webapi/common/CommonWebApiResponse.java" , "src/main/java/ksbysample/webapp/lending/webapi/weather" ]; def excludeFileNamePatterns = [ ".*EventListener.java" , ".*Form.java" , ".*Values.java" ]; compareSrcAndTestDir(srcDir, excludePaths, excludeFileNamePatterns); }
, "src/main/java/ksbysample/webapp/lending/service/calilapi/Librar"
â, "src/main/java/ksbysample/webapp/lending/service/calilapi/response"
ã¸å¤æ´ãã¾ãã
EmailServiceTest.java
package ksbysample.webapp.lending.service; import ksbysample.common.test.MailServerResource; import ksbysample.webapp.lending.Application; 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.mail.javamail.JavaMailSender; import org.springframework.mail.javamail.MimeMessageHelper; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import org.springframework.test.context.web.WebAppConfiguration; import javax.mail.internet.InternetAddress; import javax.mail.internet.MimeMessage; import static org.hamcrest.Matchers.is; import static org.junit.Assert.assertThat; @RunWith(SpringJUnit4ClassRunner.class) @SpringApplicationConfiguration(classes = Application.class) @WebAppConfiguration public class EmailServiceTest { @Rule @Autowired public MailServerResource mailServer; @Autowired private JavaMailSender mailSender; @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("ããã¯ãã¹ãã§ã")); } @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("ããã¯ãã¹ãã§ã"); emailService.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("ããã¯ãã¹ãã§ã")); } }
lending/helper/mail/assertdata/001/message.txt
貸åºç¶æ³ã確èªãã¾ããã以ä¸ã®URLããåãããæ¸ç±ãç³è«ãã¦ãã ããã http://localhost:8080/lendingapp?lendingAppId=1
Mail001HelperTest.java
package ksbysample.webapp.lending.helper.mail; import com.google.common.base.Charsets; import ksbysample.webapp.lending.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 javax.mail.Message; import javax.mail.internet.MimeMessage; import java.io.File; import static org.assertj.core.api.Assertions.assertThat; @RunWith(SpringJUnit4ClassRunner.class) @SpringApplicationConfiguration(classes = Application.class) @WebAppConfiguration public class Mail001HelperTest { @Autowired private Mail001Helper mail001Helper; @Test public void testCreateMessage() throws Exception { MimeMessage message = mail001Helper.createMessage("[email protected]", 1L); assertThat(message.getRecipients(Message.RecipientType.TO)) .extracting(Object::toString) .containsOnly("[email protected]"); assertThat(message.getContent()) .isEqualTo(com.google.common.io.Files.toString( new File("src/test/resources/ksbysample/webapp/lending/helper/mail/assertdata/001/message.txt") , Charsets.UTF_8)); } }
- To ã¢ãã¬ã¹ã®ãã§ãã¯ã«ããã¦
message.getRecipients(Message.RecipientType.TO)
㯠Address[] ã®ãã¼ã¿ãè¿ãã®ã§ãAssertJ ã® extracting ã¡ã½ããã使ç¨ã㦠String ã¸å¤æãã¦ãããã§ãã¯ãã¦ãã¾ãã - æ¬æã®ãã§ãã¯ã¯ãã¹ããã¼ã¿ãå¥ãã¡ã¤ã«ã«ç¨æãã¦ãã㦠Guava ã® Files.toString ã使ç¨ã㦠String ã®ãã¼ã¿ãåå¾ãã¦ãã§ãã¯ãã¦ãã¾ãã
listener/rabbitmq/testdata/001/table-ordering.txt, lending_app.csv, lending_book.csv
â table-ordering.txt
lending_app lending_book
â lending_app.csv
lending_app_id,status,lending_user_id,approval_user_id 1,1,1,[null]
â lending_book.csv
lending_book_id,lending_app_id,isbn,book_name,lending_state,lending_app_flg,lending_app_reason,approval_result,approval_reason 1,1,978-4-7741-6366-6,GitHubå®è·µå ¥é,[null],[null],[null],[null],[null] 2,1,978-4-7741-5377-3,JUnitå®è·µå ¥é,[null],[null],[null],[null],[null] 3,1,978-4-7973-8014-9,Javaæå¼·ãªãã¡ã¬ã³ã¹,[null],[null],[null],[null],[null] 4,1,978-4-7973-4778-4,ã¢ã¸ã£ã¤ã«ã½ããã¦ã§ã¢éçºã®å¥¥ç¾©,[null],[null],[null],[null],[null] 5,1,978-4-87311-704-1,Javaã«ããé¢æ°åããã°ã©ãã³ã°,[null],[null],[null],[null],[null]
listener/rabbitmq/assertdata/001/table-ordering.txt, lending_app.csv, lending_book.csv
â table-ordering.txt
lending_app lending_book
â lending_app.csv
lending_app_id,status,lending_user_id,approval_user_id 1,2,1,[null]
â lending_book.csv
lending_book_id,lending_app_id,isbn,book_name,lending_state,lending_app_flg,lending_app_reason,approval_result,approval_reason 1,1,978-4-7741-6366-6,GitHubå®è·µå ¥é,貸åºå¯,[null],[null],[null],[null] 2,1,978-4-7741-5377-3,JUnitå®è·µå ¥é,èµæ¸ãã,[null],[null],[null],[null] 3,1,978-4-7973-8014-9,Javaæå¼·ãªãã¡ã¬ã³ã¹,貸åºä¸,[null],[null],[null],[null] 4,1,978-4-7973-4778-4,ã¢ã¸ã£ã¤ã«ã½ããã¦ã§ã¢éçºã®å¥¥ç¾©,æºåä¸,[null],[null],[null],[null] 5,1,978-4-87311-704-1,Javaã«ããé¢æ°åããã°ã©ãã³ã°,èµæ¸ãªã,[null],[null],[null],[null]
Book.java, SystemData.java, Libkey.java
â Book.java
@JsonIgnoreProperties(ignoreUnknown = true) @Data @ToString @NoArgsConstructor @AllArgsConstructor public class Book {
- @NoArgsConstructor, @AllArgsConstructor ã¢ããã¼ã·ã§ã³ãã¯ã©ã¹ã«ä»å ãã¾ãã
â SystemData.java
@JsonIgnoreProperties(ignoreUnknown = true) @Data @ToString @NoArgsConstructor @AllArgsConstructor public class SystemData {
- @NoArgsConstructor, @AllArgsConstructor ã¢ããã¼ã·ã§ã³ãã¯ã©ã¹ã«ä»å ãã¾ãã
â Libkey.java
@JsonIgnoreProperties(ignoreUnknown = true) @Data @ToString @NoArgsConstructor @AllArgsConstructor public class Libkey {
- @NoArgsConstructor, @AllArgsConstructor ã¢ããã¼ã·ã§ã³ãã¯ã©ã¹ã«ä»å ãã¾ãã
InquiringStatusOfBookQueueListenerTest.java
package ksbysample.webapp.lending.listener.rabbitmq; import com.google.common.base.Charsets; import ksbysample.common.test.*; import ksbysample.webapp.lending.Application; import ksbysample.webapp.lending.dao.LibraryForsearchDao; import ksbysample.webapp.lending.entity.LibraryForsearch; import ksbysample.webapp.lending.service.calilapi.CalilApiService; import ksbysample.webapp.lending.service.calilapi.response.Book; import ksbysample.webapp.lending.service.calilapi.response.Libkey; import ksbysample.webapp.lending.service.calilapi.response.SystemData; import ksbysample.webapp.lending.service.queue.InquiringStatusOfBookQueueMessage; import mockit.Mock; import mockit.MockUp; import org.dbunit.dataset.IDataSet; import org.dbunit.dataset.csv.CsvDataSet; import org.junit.Rule; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.amqp.core.Message; import org.springframework.amqp.core.MessageProperties; import org.springframework.amqp.support.converter.MessageConverter; 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 javax.mail.internet.MimeMessage; import javax.sql.DataSource; import java.io.File; import java.util.ArrayList; import java.util.Arrays; import java.util.List; import static mockit.Deencapsulation.getField; import static mockit.Deencapsulation.setField; import static org.assertj.core.api.Assertions.assertThat; @RunWith(SpringJUnit4ClassRunner.class) @SpringApplicationConfiguration(classes = Application.class) @WebAppConfiguration public class InquiringStatusOfBookQueueListenerTest { @Rule @Autowired public TestDataResource testDataResource; @Rule @Autowired public TestDataLoaderResource testDataLoaderResource; @Rule @Autowired public MailServerResource mailServer; @Autowired private DataSource dataSource; @Autowired private MessageConverter messageConverter; @Autowired private InquiringStatusOfBookQueueListener listener; @Test @TestDataLoader("src/test/resources/ksbysample/webapp/lending/listener/rabbitmq/testdata/001") public void testReceiveMessage() throws Exception { // ã¢ãã¯ã«å ¥ãæ¿ããåã®ãã£ã¼ã«ãã®å®ä½ãéé¿ãã LibraryForsearchDao libraryForsearchDaoOrg = getField(listener, "libraryForsearchDao"); CalilApiService calilApiServiceOrg = getField(listener, "calilApiService"); try { /** * ã¢ãã¯å®ç¾©é¨ */ // InquiringStatusOfBookQueueListener.libraryForsearchDao ãã¢ãã¯ã«å ¥ãæ¿ãã LibraryForsearchDao libraryForsearchDao = new MockUp<LibraryForsearchDao>() { @Mock LibraryForsearch selectSelectedLibrary() { LibraryForsearch libraryForsearch = new LibraryForsearch(); libraryForsearch.setSystemid("System_Id"); libraryForsearch.setFormal("å³æ¸é¤¨å"); return libraryForsearch; } }.getMockInstance(); setField(listener, "libraryForsearchDao", libraryForsearchDao); // InquiringStatusOfBookQueueListener.calilApiService ãã¢ãã¯ã«å ¥ãæ¿ãã CalilApiService calilApiService = new MockUp<CalilApiService>() { @Mock public List<Book> check(String systemid, List<String> isbnList) { List<Book> bookList = new ArrayList<>(); bookList.add(new Book("978-4-7741-6366-6", null, new SystemData(null, null, null, Arrays.asList(new Libkey(null, "貸åºå¯"))))); bookList.add(new Book("978-4-7741-5377-3", null, new SystemData(null, null, null, Arrays.asList(new Libkey(null, "èµæ¸ãã"))))); bookList.add(new Book("978-4-7973-8014-9", null, new SystemData(null, null, null, Arrays.asList(new Libkey(null, "貸åºä¸"))))); bookList.add(new Book("978-4-7973-4778-4", null, new SystemData(null, null, null, Arrays.asList(new Libkey(null, "æºåä¸"))))); bookList.add(new Book("978-4-87311-704-1", null, new SystemData(null, null, null, Arrays.asList(new Libkey(null, "èµæ¸ãªã"))))); return bookList; } }.getMockInstance(); setField(listener, "calilApiService", calilApiService); /** * ãã¹ãæ¬ä½ */ InquiringStatusOfBookQueueMessage queueMessage = new InquiringStatusOfBookQueueMessage(); queueMessage.setLendingAppId(1L); Message message = messageConverter.toMessage(queueMessage, new MessageProperties()); listener.receiveMessage(message); /** * æ¤è¨¼ */ // ãã¼ãã«ã®ä»¥ä¸ã®ã«ã©ã ã®ãã¼ã¿ãæ¤è¨¼ãã // ã»lending_app.status // ã»lending_book.lending_state IDataSet dataSet = new CsvDataSet(new File("src/test/resources/ksbysample/webapp/lending/listener/rabbitmq/assertdata/001")); TableDataAssert tableDataAssert = new TableDataAssert(dataSet, dataSource); tableDataAssert.assertEquals("lending_app", new String[]{"lending_app_id", "lending_user_id", "approval_user_id"}); tableDataAssert.assertEquals("lending_book", new String[]{"lending_book_id", "lending_app_id", "isbn", "book_name", "lending_app_flg", "lending_app_reason", "approval_result", "approval_reason"}); // éä¿¡ãããã¡ã¼ã«ãæ¤è¨¼ãã assertThat(mailServer.getMessagesCount()).isEqualTo(1); MimeMessage mimeMessage = mailServer.getFirstMessage(); assertThat(mimeMessage.getRecipients(javax.mail.Message.RecipientType.TO)) .extracting(Object::toString) .containsOnly("[email protected]"); assertThat(mimeMessage.getContent()) .isEqualTo(com.google.common.io.Files.toString( new File("src/test/resources/ksbysample/webapp/lending/helper/mail/assertdata/001/message.txt") , Charsets.UTF_8)); } finally { // ã¢ãã¯ã«å·®ãæ¿ãããã£ã¼ã«ããéé¿ãã¦ãããå ã®å®ä½ã«æ»ã setField(listener, "libraryForsearchDao", libraryForsearchDaoOrg); setField(listener, "calilApiService", calilApiServiceOrg); } } }
- ä»åã®ãã¹ãã§ã¯ãInquiringStatusOfBookQueueListener ã¯ã©ã¹ã® receiveMessage ã¡ã½ããå
ã®ä»¥ä¸ã®å¦çã®ã¿ã¢ãã¯ã«å
¥ãæ¿ãã¦ãã¾ãã
- libraryForsearchDao.selectSelectedLibrary()
- calilApiService.check(...)
LendingUserDetailsHelperTest.java
package ksbysample.webapp.lending.security; import ksbysample.common.test.TestDataResource; import ksbysample.webapp.lending.Application; import ksbysample.webapp.lending.dao.UserInfoDao; import ksbysample.webapp.lending.entity.UserInfo; 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.security.authentication.TestingAuthenticationToken; import org.springframework.security.core.Authentication; import org.springframework.security.core.context.SecurityContextHolder; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import org.springframework.test.context.web.WebAppConfiguration; import static org.assertj.core.api.Assertions.assertThat; @RunWith(SpringJUnit4ClassRunner.class) @SpringApplicationConfiguration(classes = Application.class) @WebAppConfiguration public class LendingUserDetailsHelperTest { @Rule @Autowired public TestDataResource testDataResource; @Autowired private UserInfoDao userInfoDao; @Test public void testGetLoginUserId() throws Exception { UserInfo userInfo = userInfoDao.selectById(1L); LendingUserDetails lendingUserDetails = new LendingUserDetails(userInfo, null); Authentication auth = new TestingAuthenticationToken(lendingUserDetails, null); SecurityContextHolder.getContext().setAuthentication(auth); assertThat(LendingUserDetailsHelper.getLoginUserId()).isEqualTo(1L); } }
UploadBooklistFormValidatorTest.java
package ksbysample.webapp.lending.web.booklist; import com.univocity.parsers.csv.CsvWriter; import com.univocity.parsers.csv.CsvWriterSettings; import ksbysample.webapp.lending.Application; import ksbysample.webapp.lending.service.file.BooklistCsvFileServiceTest; 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.mock.web.MockMultipartFile; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import org.springframework.test.context.web.WebAppConfiguration; import org.springframework.validation.Errors; import org.springframework.validation.MapBindingResult; import org.springframework.validation.ObjectError; import org.springframework.web.multipart.MultipartFile; import java.io.BufferedWriter; import java.io.InputStream; import java.nio.charset.Charset; import java.nio.file.Files; import java.nio.file.Path; import java.util.HashMap; import static org.assertj.core.api.Assertions.assertThat; import static org.junit.Assert.*; @RunWith(SpringJUnit4ClassRunner.class) @SpringApplicationConfiguration(classes = Application.class) @WebAppConfiguration public class UploadBooklistFormValidatorTest { @Autowired private UploadBooklistFormValidator uploadBooklistFormValidator; private BooklistCsvFileServiceTest booklistCsvFileServiceTest = new BooklistCsvFileServiceTest(); @Test public void testValidate_NoErrorCsvFile() throws Exception { UploadBooklistForm uploadBooklistForm = new UploadBooklistForm(); MultipartFile multipartFile = booklistCsvFileServiceTest.createNoErrorCsvFile(); uploadBooklistForm.setFileupload(multipartFile); Errors errors = new MapBindingResult(new HashMap<String, String>(), ""); uploadBooklistFormValidator.validate(uploadBooklistForm, errors); assertThat(errors.hasErrors()).isFalse(); } @Test public void testValidate_ErrorCsvFile() throws Exception { UploadBooklistForm uploadBooklistForm = new UploadBooklistForm(); MultipartFile multipartFile = booklistCsvFileServiceTest.createErrorCsvFile(); uploadBooklistForm.setFileupload(multipartFile); Errors errors = new MapBindingResult(new HashMap<String, String>(), ""); uploadBooklistFormValidator.validate(uploadBooklistForm, errors); assertThat(errors.hasErrors()).isTrue(); assertThat(errors.getErrorCount()).isEqualTo(6); assertThat(errors.getAllErrors()) .contains(new ObjectError("", new String[]{"UploadBooklistForm.fileupload.lengtherr"}, new Object[]{2, 3}, null)) .contains(new ObjectError("", new String[]{"UploadBooklistForm.fileupload.isbn.patternerr"}, new Object[]{3, "978-4-7741-5x77-3"}, null)) .contains(new ObjectError("", new String[]{"UploadBooklistForm.fileupload.isbn.lengtherr"}, new Object[]{4, "978-4-79173-8014-9"}, null)) .contains(new ObjectError("", new String[]{"UploadBooklistForm.fileupload.isbn.numlengtherr"}, new Object[]{4, "97847917380149"}, null)) .contains(new ObjectError("", new String[]{"UploadBooklistForm.fileupload.isbn.numlengtherr"}, new Object[]{5, "97847197347784"}, null)) .contains(new ObjectError("", new String[]{"UploadBooklistForm.fileupload.bookname.lengtherr"}, new Object[]{6, "123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789"}, null)); } }
- ãã¹ãã§ä½¿ç¨ãã CSV ãã¡ã¤ã«ã®ä½æã«ã¯ BooklistCsvFileServiceTest ã¯ã©ã¹ã®ã¡ã½ãããå©ç¨ãã¾ã ( æ¬æ¥ã¯å ±éã¯ã©ã¹ã« CSV ãã¡ã¤ã«çæç¨ã®ã¡ã½ããã移åããæ¹ãããã¨æãã¾ããä»åã¯ããã§é²ãã¾ã )ã
å±¥æ´
2015/11/07
åççºè¡ã
2015/11/08
* ãã¡ã¤ã«ã¢ãããã¼ãæã«ã¨ã©ã¼ãåºããã¨ã«æ°ã¥ãããããBook, SystemData, Libkey ã¯ã©ã¹ã« @AllArgsConstructor 以å¤ã« @NoArgsConstructor ãä»å ããããä¿®æ£ãã¾ããã