Spring Boot ã§æ¸ç±ã®è²¸åºç¶æ³ç¢ºèªã»è²¸åºç³è«ãã Web ã¢ããªã±ã¼ã·ã§ã³ãä½ã ( ãã®ï¼ï¼ )( 貸åºå¸ææ¸ç± CSV ãã¡ã¤ã«ã¢ãããã¼ãç»é¢ã®ä½æï¼ )
æ¦è¦
- ä»åã®æé ã§ç¢ºèªã§ããã®ã¯ä»¥ä¸ã®å
容ã§ãã
- 貸åºå¸ææ¸ç± CSV ãã¡ã¤ã«ã¢ãããã¼ãç»é¢ã®ä½æ
- ãã¹ãã®ä½æã®ç¶ãã§ãã
- 貸åºå¸ææ¸ç± CSV ãã¡ã¤ã«ã¢ãããã¼ãç»é¢ã®ä½æ
åç §ãããµã¤ãã»æ¸ç±
-
spring-amqp/spring-rabbit/src/test/java/org/springframework/amqp/rabbit/
https://github.com/spring-projects/spring-amqp/tree/master/spring-rabbit/src/test/java/org/springframework/amqp/rabbit- Spring AMQP ã® RabbitMQ ç¨ã©ã¤ãã©ãªã® GitHub ã«ãã test ãã£ã¬ã¯ããªã§ããSpring AMQP + RabbitMQ ã®ãã¹ãã®æ¹æ³ãåãããªãã£ãã®ã§åèã«ãã¾ããã
-
Java Code Examples for org.springframework.amqp.rabbit.core.RabbitAdmin
http://www.programcreek.com/java-api-examples/index.php?api=org.springframework.amqp.rabbit.core.RabbitAdmin- Spring AMQP + RabbitMQ ã®ãã¹ãã®æ¹æ³ã«ã¤ãã¦ãã¡ãã®ãµã¤ããåèã«ãã¾ããã
- ãã®ãµã¤ãã¯ãµã³ãã«ãè±å¯ãªæ°ããã¾ããæè¿ããè¦ã«è¡ãã¾ãã
The JMockit Testing Toolkit Tutorial
http://jmockit.org/tutorial.html- JMockit ã® Tutorial ã§ããã¯ã©ã¹ã® private ã¡ã½ããã Mock ã¡ã½ããã«ããæ¹æ³ãåç §ãã¾ããã
Java Code Examples for org.springframework.test.web.servlet.request.MockMvcRequestBuilders
http://www.programcreek.com/java-api-examples/index.php?api=org.springframework.test.web.servlet.request.MockMvcRequestBuilders- MockMvcRequestBuilders ã«ãããã¡ã¤ã«ã¢ãããã¼ãã®ãã¹ãã®æ¸ãæ¹ãåç §ãã¾ãããExample 8 ã§ãã
ç®æ¬¡
- InquiringStatusOfBookQueueService ã¯ã©ã¹ã®ãã¹ãã®ä½æ
- BooklistService ã¯ã©ã¹ã®ãã¹ãã®ä½æ
- BooklistController ã¯ã©ã¹ã®ãã¹ãã®ä½æ
- å ¨ã¦ã®ãã¹ããæåããã確èªãã
- commitãPushãPull Requestããã¼ã¸
- 次åã¯ããã
- ã¡ã¢æ¸ã
æé
InquiringStatusOfBookQueueService ã¯ã©ã¹ã®ãã¹ãã®ä½æ
src/main/java/ksbysample/webapp/lending/service/queue ã®ä¸ã® InquiringStatusOfBookQueueService.java ã§ãCreate Testããã¤ã¢ãã°ã表示ãããã¹ãã¯ã©ã¹ãä½æãã¾ãã
src/test/java/ksbysample/webapp/lending/service/queue ã®ä¸ã« InquiringStatusOfBookQueueServiceTest.java ãä½æããã¾ãã
src/test/java/ksbysample/webapp/lending/service/queue ã®ä¸ã® InquiringStatusOfBookQueueServiceTest.java ã ãªã³ã¯å ã®å 容 ã«å¤æ´ãã¾ãã
ãã¹ããå®è¡ãã¾ããInquiringStatusOfBookQueueServiceTest ã¯ã©ã¹ã®ã¯ã©ã¹åã«ã«ã¼ã½ã«ã移åããã³ã³ããã¹ãã¡ãã¥ã¼ã表示å¾ãRun 'InquiringStatusOfBookQueueServ...' with Coverageããé¸æãã¾ãã
ãã¹ããæåãããã¨ã確èªã§ãã¾ãã
BooklistService ã¯ã©ã¹ã®ãã¹ãã®ä½æ
src/main/java/ksbysample/webapp/lending/web/booklist ã®ä¸ã® BooklistService.java ã§ãCreate Testããã¤ã¢ãã°ã表示ãããã¹ãã¯ã©ã¹ãä½æãã¾ãã
src/test/java/ksbysample/webapp/lending/web/booklist ã®ä¸ã« BooklistServiceTest.java ãä½æããã¾ãã
lending_app, lending_book ãã¼ãã«ããã¹ããã¼ã¿ã«å ¥ãæ¿ããããã®è¨è¿°ã table-ordering.txt ã«è¨è¿°ããã®ãå¿ãã¦ããã®ã§è¿½å ãã¾ããsrc/test/resources/testdata/base ã®ä¸ã® table-ordering.txt ã ãªã³ã¯å ã®å 容 ã«å¤æ´ãã¾ãã
ï¼ç¹æ³¨æäºé ã¨ã㦠table-ordering.txt ã¯å¤é¨ãã¼å¶ç´ã§è¦ªåé¢ä¿ããããã¼ãã«ã¯åãã¼ãã«ãä¸ã«æ¸ãããã«ãã¾ããããããªãã¨ãã¼ãã«ã®ãã¼ã¿ãåé¤ãããã¨ãã§ãããã¹ãå®è¡æã«ã¨ã©ã¼ãçºçãã¾ãã
ã¢ãããã¼ããã¡ã¤ã«ã® MockMultipartFile ã¯ã©ã¹ã®ã¤ã³ã¹ã¿ã³ã¹ãçæããããã« BooklistCsvFileServiceTest ã®ã¡ã½ãããå©ç¨ã§ããããã«ãã¾ããsrc/test/java/ksbysample/webapp/lending/service/file ã®ä¸ã® BooklistCsvFileServiceTest.java ã ãªã³ã¯å ã®å 容 ã«å¤æ´ãã¾ãã
ä»ã®ã¾ã¾ã ã¨ãã¹ããã«ãããã¨ã«æ°ã¥ããã®ã§ãã¹ãããããæ§æã«å¤æ´ãã¾ããã¾ã src/main/java/ksbysample/webapp/lending/security ã®ä¸ã« LendingUserDetailsHelper.java ãä½æãã¾ããä½æå¾ããªã³ã¯å ã®å 容 ã«å¤æ´ãã¾ãã
次㫠src/main/java/ksbysample/webapp/lending/web/booklist ã®ä¸ã® BooklistService.java ã ãªã³ã¯å ã®ãã®ï¼ã®å 容 ã«å¤æ´ãã¾ãã
ãã¼ã¿ç»é²å¾ã®ç¢ºèªç¨ãã¼ã¿ãä½æãã¾ããsrc/test/resources/ksbysample/webapp/lending/web ã®ä¸ã« booklist/assertdata/001 ãã£ã¬ã¯ããªãä½æãã¾ãã
src/test/resources/ksbysample/webapp/lending/web/booklist/assertdata/001 ã®ä¸ã« table-ordering.txt, lending_app.csv, lending_book.csv ãä½æãã¾ããä½æå¾ããªã³ã¯å ã®å 容 ã«å¤æ´ãã¾ãã
BooklistController ã¯ã©ã¹ãBooklistService ã¯ã©ã¹ã§å®è£ æã«ã¡ã½ããåã¨å¦çå 容ãï¼è»¢ãï¼è»¢ããã¦ãã¦ä¿®æ£ãå¿ãã¦ããé¨åããããã¨ã«æ°ã¥ããã®ã§ä¿®æ£ãã¾ããsrc/main/java/ksbysample/webapp/lending/web/booklist ã®ä¸ã® BooklistService.java ã ãªã³ã¯å ã®ãã®ï¼ã®å 容 ã«å¤æ´ãã¾ãã
src/main/java/ksbysample/webapp/lending/web/booklist ã®ä¸ã® BooklistController.java ã ãªã³ã¯å ã®å 容 ã«å¤æ´ãã¾ãã
src/test/java/ksbysample/webapp/lending/web/booklist ã®ä¸ã® BooklistServiceTest.java ã ãªã³ã¯å ã®å 容 ã«å¤æ´ãã¾ãã
ãã¹ããå®è¡ãã¾ããBooklistServiceTest ã¯ã©ã¹ã®ã¯ã©ã¹åã«ã«ã¼ã½ã«ã移åããã³ã³ããã¹ãã¡ãã¥ã¼ã表示å¾ãRun 'BooklistServiceTest' with Coverageããé¸æãã¾ãã
ãã¹ããæåãããã¨ã確èªã§ãã¾ãã
BooklistController ã¯ã©ã¹ã®ãã¹ãã®ä½æ
src/main/java/ksbysample/webapp/lending/web/booklist ã®ä¸ã® BooklistController.java ã§ãCreate Testããã¤ã¢ãã°ã表示ãããã¹ãã¯ã©ã¹ãä½æãã¾ãã
src/test/java/ksbysample/webapp/lending/web/booklist ã®ä¸ã« BooklistControllerTest.java ãä½æããã¾ãã
æåã«ãã¹ãã®æ§æã決ãã¾ããsrc/test/java/ksbysample/webapp/lending/web/booklist ã®ä¸ã® BooklistControllerTest.java ã ãªã³ã¯å ã®ãã®ï¼ã®å 容 ã«å¤æ´ãã¾ãã
ãã¹ããå®è£ ãã¾ããsrc/test/java/ksbysample/webapp/lending/web/booklist ã®ä¸ã® BooklistControllerTest.java ã ãªã³ã¯å ã®ãã®ï¼ã®å 容 ã«å¤æ´ãã¾ãã
ãã¹ããå®è¡ãã¾ããBooklistControllerTest ã¯ã©ã¹ã®ã¯ã©ã¹åã«ã«ã¼ã½ã«ã移åããã³ã³ããã¹ãã¡ãã¥ã¼ã表示å¾ãRun 'BooklistControllerTest' with Coverageããé¸æãã¾ãã
ãã¹ããæåãããã¨ã確èªã§ãã¾ãã
å ¨ã¦ã®ãã¹ããæåããã確èªãã
æå¾ã«å ¨ã¦ã®ãã¹ããæåããã確èªãã¾ããProject View ã®ã«ã¼ãã§ã³ã³ããã¹ãã¡ãã¥ã¼ã表示ãã¦ãRun 'Tests in 'ksbysample...' with Coverageããé¸æãã¾ãã
OpenWeatherMapApiServiceTest ã ãã¨ã©ã¼ã«ãªãã¾ããããã°ãè¦ã㨠"401 Unauthorized" ã¨åºåããã¦ããã®ã§ãã©ããèªè¨¼ãå¿ è¦ã«ãªã£ãããã§ãã
http://openweathermap.org/api ãè¦ã㨠"To access the API you need to sign up for an API key if you are on a free or paid plan." ã¨æ¸ããã¦ãã¾ãããç¹ã«ã¡ã¤ã³ã®æ©è½ã¨ã¯é¢ä¿ããªãã®ã§ãä»åã¯ãã¹ããå®è¡å¯¾è±¡å¤ã«ãã¾ãã
src/test/java/ksbysample/webapp/lending/service/openweathermapapi ã®ä¸ã® OpenWeatherMapApiServiceTest.java ã ãªã³ã¯å ã®å 容 ã«å¤æ´ãã¾ãã
ååº¦å ¨ã¦ã®ãã¹ããå®è¡ããä»åº¦ã¯æåãããã¨ã確èªã§ãã¾ãã
â»UniVocityParsersTest ã¯ã©ã¤ãã©ãªã®æ¤è¨¼ã®ããã«ä½æãããã¹ããªã®ã§ããã¡ãã対象å¤ã«ãã¾ãããQiita ã«è¨äºãæ¸ããã®ã§ã詳細㯠Java ã® CSV/TSV/åºå®é·ãã¡ã¤ã« Parser ã©ã¤ãã©ãª uniVocity-parsers ã®ä½¿ãæ¹ ãã覧ãã ããã
â»"(Coverage): Error during class instrumentation: org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory: java.lang.NullPointerException" ã¨ãããã°ãåºã¦ãããstackoverflow ã® Error trying to wire a Lettuce Connection Factory ã®è¨äºãåèã«ããã°è§£æ±ºãããããªæ°ããã¾ãããä¸æ¦ç¡è¦ãã¾ãã
commitãPushãPull Requestããã¼ã¸
ããã¾ã§ã®å¤æ´å 容ã commit ãã¾ãã
ã³ãã³ãã©ã¤ã³ãã以ä¸ã®ã³ãã³ããå®è¡ã㦠commit ãï¼ã¤ã«ã¾ã¨ãã¾ãã
> git rebase -i HEAD~8
> git commit --amend -m "#29 貸åºå¸ææ¸ç± CSV ãã¡ã¤ã«ã¢ãããã¼ãç»é¢ãä½æãã¾ããã"GitHub 㸠Pushã1.0.x-make-booklist -> 1.0.x 㸠Pull Requestã1.0.x ã§ãã¼ã¸ã1.0.x-make-booklist ãã©ã³ããåé¤ãããã¾ãã
clean ã¿ã¹ã¯ã®å®è¡âãRebuild Projectãã¡ãã¥ã¼ã®å®è¡âbuild ã¿ã¹ã¯ã®å®è¡ãè¡ãã"BUILD SUCCESSFUL" ã®ã¡ãã»ã¼ã¸ãåºåããããã®ç¢ºèªãå¿ãã¦ããã®ã§ãããã§ç¢ºèªãã¾ãã
次åã¯ããã
æè¿éçºã§ä½¿ç¨ããããã«æ¸ãã¦ããè¨äºãèªã¿è¿ãã¦ãã¦ã以ä¸ã®ï¼ç¹ãåããã¥ããã£ãã®ã§æ¹ãã¦ã¾ã¨ãç´ãã¦ã¿ã¾ãã
- Spring Boot 㧠Doma 2 ã使ç¨ããã«ã¯
- èªä½ãããã¹ãç¨ã¯ã©ã¹ ( src/test/java/ksbysample/common/test ) ã®ä½¿ãæ¹
ãã®å¾ã§ Git for Windows ã 2.6.1 ã«ãã¼ã¸ã§ã³ã¢ããããå¾ã次ã®è²¸åºç¶æ³åå¾ã¿ã¹ã¯ã®ä½æã«é²ã¿ã¾ãã
ã¡ã¢æ¸ã
@Autowired 㧠DI ããã¤ã³ã¹ã¿ã³ã¹ã® private ã¡ã½ããã®å¦çã ãã JMockit 㧠Mock ã«åºæ¥ãã®ãï¼
çµè«ã¨ãã¦ãåºæ¥ã¾ãããã ãä¸ã§ãã¹ããä½ã£ã¦ããæã«ã¯åããã¾ããã§ããã
BooklistService ã¯ã©ã¹ã® temporarySaveBookListCsvFile ã¡ã½ããã®ä¸ã§ç¾å¨ãã°ã¤ã³ä¸ã®ã¦ã¼ã¶ã® userId ãåå¾ããå¦çãããã«ãã¼ã¯ã©ã¹ã® LendingUserDetailsHelper.getLoginUserId() ã§ã¯ãªããBooklistService ã¯ã©ã¹å ã« getLoginUserId ã¨ãã private ã¡ã½ãããç¨æãã¦ãBooklistServiceTest ã§ä»¥ä¸ã®ããã«å®è£ ã㦠getLoginUserId ã¡ã½ããã®å¦çã ã Mock ã«ãããã¨æåæã£ã¦ããã®ã§ããããã®æ¹æ³ã§ã¯ãã¡ã§ããã
@Autowired private BooklistService booklistService; @Test public void testTemporarySaveBookListCsvFile() throws Exception { new NonStrictExpectations(booklistService) {{ invoke(booklistService, "getLoginUserId"); result = new Long(1); }};
invoke ãå¼ã³åºã㨠booklistService ã®ã¤ã³ã¹ã¿ã³ã¹ã Mock ã«å¤ãããtemporarySaveBookListCsvFile ã¡ã½ããã¾ã§ Mock ã¡ã½ããã«ãªã£ã¦å®éã®å¦çãå¼ã³åºããã¾ãããtemporarySaveBookListCsvFile ã¡ã½ããå ã«ãã¬ã¼ã¯ãã¤ã³ããè¨å®ãã¦ãããã°ãã¦ãééãã¾ããã§ããã
JMockit ã® Tutorial ã® Mocking ã®æ¹ã°ããè¦ã¦ããã®ã§æ£ããæ¹æ³ã«æ°ã¥ããªãã£ãã®ã§ãããFaking ã®æ¹ãè¦ã¦ããæ¹ãåããã¾ããã
以ä¸ã®å®è£ 㧠private ã¡ã½ããã®ã¿ Mock ã¡ã½ããã«ã§ãã¾ãã
@Autowired private BooklistService booklistService; public class MockBooklistService extends MockUp<BooklistService> { @Mock private Long getLoginUserId() { return new Long(1); } } @Test public void testTemporarySaveBookListCsvFile() throws Exception { new MockBooklistService(); UploadBooklistForm uploadBooklistForm = new UploadBooklistForm();
ãã¤ã³ãã¯ä»¥ä¸ã®éãã§ãã
- @Autowired 㧠DI ãã¦ããé¨åã¯ãã®ã¾ã¾ã§æ§ãã¾ããã
- MockUp ãå©ç¨ãã¦ä¸é¨ã®ã¡ã½ããã Mock ã¡ã½ããã«ãããã¯ã©ã¹ã® Mock ã¯ã©ã¹ãå®ç¾©ãã¾ãã
- å®ç¾©ãã Mock ã¯ã©ã¹ã®ä¸ã§ Mock ã¡ã½ããã @Mock ã¢ããã¼ã·ã§ã³ãä»å ãã¦è¨è¿°ãã¾ããããã«æ¸ãã¦ããªãã¡ã½ããã¯æ¬æ¥ã®ã¡ã½ãããå¼ã³åºããã¾ãã
- æå¾ã«ãã¹ãã¡ã½ããã®æåã§ããã® Mock ã¯ã©ã¹ã new ãã¾ãã
- Mock ã¯ã©ã¹ã new ãã¦ããªããã¹ãã¡ã½ããã§ã¯ BooklistService ã®ã¡ã½ãã㯠Mock ã¡ã½ããã«ã¯ãªãã¾ãã ( æ¬æ¥ã®ã¡ã½ãããå¼ã³åºããã¾ã )ã
ãã ãã¾ã ããåãããªããã¨ãããã¾ããä¸ã®æ¹æ³ã§ getLoginUserId ã¡ã½ããã ã Mock ã¡ã½ããã«ãã¦ãã以å¤ã¯æ¬æ¥ã®ã¡ã½ãããå¼ã³åºããã¦ããã¯ããªã®ã§ãããBooklistService::temporarySaveBookListCsvFile ã®ä¸ã«ãã¬ã¼ã¯ãã¤ã³ããè¨å®ãã¦ãã¹ãã¡ã½ããã Debug ã¢ã¼ãã§å®è¡ãã¦ããªãããã¬ã¼ã¯ãã¤ã³ããééããªã ( æ¢ã¾ããªã ) ãã§ããããã¾ã ã¾ã åãããªããã¨ããããã ãªããããã¨æãã¾ããã
ã½ã¼ã¹ã³ã¼ã
InquiringStatusOfBookQueueServiceTest.java
package ksbysample.webapp.lending.service.queue; import ksbysample.webapp.lending.Application; import ksbysample.webapp.lending.config.Constant; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.amqp.core.Queue; import org.springframework.amqp.rabbit.connection.ConnectionFactory; import org.springframework.amqp.rabbit.core.RabbitAdmin; import org.springframework.amqp.rabbit.core.RabbitTemplate; 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 static org.assertj.core.api.Assertions.assertThat; @RunWith(SpringJUnit4ClassRunner.class) @SpringApplicationConfiguration(classes = Application.class) @WebAppConfiguration public class InquiringStatusOfBookQueueServiceTest { @Autowired private ConnectionFactory connectionFactory; @Autowired private Queue queue; @Autowired private RabbitTemplate rabbitTemplate; @Autowired private InquiringStatusOfBookQueueService inquiringStatusOfBookQueueService; @Test public void testSendMessage() throws Exception { RabbitAdmin rabbitAdmin = new RabbitAdmin(connectionFactory); rabbitAdmin.deleteQueue(Constant.QUEUE_NAME_INQUIRING_STATUSOFBOOK); rabbitAdmin.declareQueue(queue); Long lendingAppId = new Long(1); inquiringStatusOfBookQueueService.sendMessage(lendingAppId); InquiringStatusOfBookQueueMessage message = (InquiringStatusOfBookQueueMessage) rabbitTemplate.receiveAndConvert(Constant.QUEUE_NAME_INQUIRING_STATUSOFBOOK); assertThat(message.getLendingAppId()).isEqualTo(lendingAppId); } }
table-ordering.txt
user_info user_role library_forsearch lending_app lending_book
- lending_app, lending_book ã追å ãã¾ãã
BooklistCsvFileServiceTest.java
public MockMultipartFile createNoErrorCsvFile() throws Exception { ..... } public MockMultipartFile createErrorCsvFile() throws Exception { ..... }
- createNoErrorCsvFile, createErrorCsvFile ã¡ã½ãããä»ã®ãã¹ãã¯ã©ã¹ãããå©ç¨ãããã®ã§ private â public ã¸å¤æ´ãã¾ãã
LendingUserDetailsHelper.java
package ksbysample.webapp.lending.security; import org.springframework.security.core.Authentication; import org.springframework.security.core.context.SecurityContextHolder; public class LendingUserDetailsHelper { /** * ç¾å¨ãã°ã¤ã³ãã¦ããã¦ã¼ã¶ã®ã¦ã¼ã¶IDãåå¾ãã * * @return ã¦ã¼ã¶ID(user_info.user_id) */ public static Long getLoginUserId() { Authentication auth = SecurityContextHolder.getContext().getAuthentication(); LendingUserDetails lendingUserDetails = (LendingUserDetails) auth.getPrincipal(); return lendingUserDetails.getUserId(); } }
BooklistService.java
â ãã®ï¼
public Long temporarySaveBookListCsvFile(UploadBooklistForm uploadBooklistForm) { // ã¢ãããã¼ããããCSVãã¡ã¤ã«ã®ãã¼ã¿ã List ã«å¤æãã List<BooklistCSVRecord> booklistCSVRecordList = booklistCsvFileService.convertFileToList(uploadBooklistForm.getFileupload()); // lending_app ãã¼ãã«ã«ãã¼ã¿ãä¿åãã LendingApp lendingApp = new LendingApp(); lendingApp.setStatus(TENPORARY_SAVE.getValue()); lendingApp.setLendingUserId(LendingUserDetailsHelper.getLoginUserId()); lendingAppDao.insert(lendingApp); // lending_book ãã¼ãã«ã«ãã¼ã¿ãä¿åãã LendingBook lendingBook; for (BooklistCSVRecord booklistCSVRecord : booklistCSVRecordList) { lendingBook = new LendingBook(); BeanUtils.copyProperties(booklistCSVRecord, lendingBook); lendingBook.setLendingAppId(lendingApp.getLendingAppId()); lendingBookDao.insert(lendingBook); } return lendingApp.getLendingAppId(); }
- temporarySaveBookListCsvFile ã¡ã½ããã®ä»¥ä¸ã®ç¹ãå¤æ´ãã¾ãããã¹ãæã« Mock ã«ãããé¨åãå¥ã¯ã©ã¹ã®ã¡ã½ãããå¼ã³åºãããã«ãã¾ãã
- SecurityContextHolder ãå©ç¨ã㦠LendingUserDetails ã®ã¤ã³ã¹ã¿ã³ã¹ãåå¾ãã¦ããé¨åãåé¤ãã¾ãã
lendingApp.setLendingUserId(lendingUserDetails.getUserId());
âlendingApp.setLendingUserId(LendingUserDetailsHelper.getLoginUserId());
ã¸å¤æ´ãã¾ãã
â ãã®ï¼
public void sendMessageToInquiringStatusOfBookQueue(RegisterBooklistForm registerBooklistForm) { inquiringStatusOfBookQueueService.sendMessage(registerBooklistForm.getLendingAppId()); }
- ã¡ã½ããåã updateLendingAppStatusToPending â sendMessageToInquiringStatusOfBookQueue ã¸å¤æ´ãã¾ãã
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 1,1,978-4-7741-5377-3,JUnitå®è·µå ¥é,NULL,NULL,NULL,NULL,NULL 1,1,978-4-7973-8014-9,Javaæå¼·ãªãã¡ã¬ã³ã¹,NULL,NULL,NULL,NULL,NULL 1,1,978-4-7973-4778-4,ã¢ã¸ã£ã¤ã«ã½ããã¦ã§ã¢éçºã®å¥¥ç¾©,NULL,NULL,NULL,NULL,NULL 1,1,978-4-87311-704-1,Javaã«ããé¢æ°åããã°ã©ãã³ã°,NULL,NULL,NULL,NULL,NULL
BooklistController.java
@RequestMapping("/register") public String register(RegisterBooklistForm registerBooklistForm , RedirectAttributes redirectAttributes) { booklistService.sendMessageToInquiringStatusOfBookQueue(registerBooklistForm); redirectAttributes.addFlashAttribute("lendingAppId", registerBooklistForm.getLendingAppId()); return "redirect:/booklist/complete"; }
booklistService.updateLendingAppStatusToPending(registerBooklistForm);
âbooklistService.sendMessageToInquiringStatusOfBookQueue(registerBooklistForm);
ã¸å¤æ´ãã¾ãã
BooklistServiceTest.java
package ksbysample.webapp.lending.web.booklist; import ksbysample.common.test.TableDataAssert; import ksbysample.common.test.TestDataResource; import ksbysample.webapp.lending.Application; import ksbysample.webapp.lending.entity.LendingBook; import ksbysample.webapp.lending.security.LendingUserDetailsHelper; import ksbysample.webapp.lending.service.file.BooklistCsvFileServiceTest; import mockit.NonStrictExpectations; 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.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.sql.DataSource; import java.io.File; import java.util.List; import static org.assertj.core.api.Assertions.assertThat; @RunWith(SpringJUnit4ClassRunner.class) @SpringApplicationConfiguration(classes = Application.class) @WebAppConfiguration public class BooklistServiceTest { private final String MAILADDR_TANAKA_TARO = "[email protected]"; @Rule @Autowired public TestDataResource testDataResource; @Autowired private DataSource dataSource; @Autowired private BooklistService booklistService; @Test public void testTemporarySaveBookListCsvFile() throws Exception { new NonStrictExpectations(LendingUserDetailsHelper.class) {{ LendingUserDetailsHelper.getLoginUserId(); result = new Long(1); }}; UploadBooklistForm uploadBooklistForm = new UploadBooklistForm(); // ãã¹ãç¨ã®ã¦ã¼ãã£ãªãã£ã¯ã©ã¹ãä½ãã¹ãã§ãããä»åã¯ä»ã®ãã¹ãã¯ã©ã¹ã®ã¡ã½ããããã®ã¾ã¾ä½¿ãã¾ã BooklistCsvFileServiceTest booklistCsvFileServiceTest = new BooklistCsvFileServiceTest(); uploadBooklistForm.setFileupload(booklistCsvFileServiceTest.createNoErrorCsvFile()); Long lendingAppId = booklistService.temporarySaveBookListCsvFile(uploadBooklistForm); assertThat(lendingAppId).isNotEqualTo(new Long(0)); IDataSet dataSet = new CsvDataSet(new File("src/test/resources/ksbysample/webapp/lending/web/booklist/assertdata/001")); TableDataAssert tableDataAssert = new TableDataAssert(dataSet, dataSource); tableDataAssert.assertEquals("lending_app", new String[]{"lending_app_id", "approval_user_id"}); tableDataAssert.assertEquals("lending_book", new String[]{"lending_book_id", "lending_app_id", "lending_state", "lending_app_flg", "lending_app_reason", "approval_result", "approval_reason"}); List<LendingBook> lendingBookList = booklistService.getLendingBookList(lendingAppId); assertThat(lendingBookList).hasSize(5); } @Test public void testSendMessageToInquiringStatusOfBookQueue() throws Exception { // ç¾å¨ã®å®è£ ã§ã¯ InquiringStatusOfBookQueueServiceTest ãéãã°OKãªã®ã§ããã¡ãã¯å®è£ ããªã } }
- BooklistService ã¯ã©ã¹ã® temporarySaveBookListCsvFile ã¡ã½ãã㨠getLendingBookList ã¡ã½ããã¯ï¼ã¤ã®ãã¹ãã¡ã½ããå ã§ãã¹ãããæ¹ãå¹ççãªã®ã§ãå¥ã ã«åããã«ã¾ã¨ãã¾ãã
- testSendMessageToInquiringStatusOfBookQueue ãã¹ãã¡ã½ãã㯠InquiringStatusOfBookQueueServiceTest ã® testSendMessage ãã¹ãã¡ã½ããã§ãã¹ããããå 容ãã«ãã¼ã§ãã¦ããã®ã§ãå®è£ ãã¾ããã
BooklistControllerTest.java
â ãã®ï¼
package ksbysample.webapp.lending.web.booklist; import ksbysample.webapp.lending.Application; import org.junit.Test; import org.junit.experimental.runners.Enclosed; import org.junit.runner.RunWith; import org.springframework.boot.test.SpringApplicationConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import org.springframework.test.context.web.WebAppConfiguration; @RunWith(Enclosed.class) public class BooklistControllerTest { @RunWith(SpringJUnit4ClassRunner.class) @SpringApplicationConfiguration(classes = Application.class) @WebAppConfiguration public static class ã¦ã¼ã¶æ¨©éã¨URLã®å¼ã³åºãå¯å¦ã®ãã¹ã { @Test public void ãã°ã¤ã³ãã¦ããªããã°è²¸åºå¸ææ¸ç±CSVãã¡ã¤ã«ã¢ãããã¼ãç»é¢ã¯è¡¨ç¤ºã§ããªã() throws Exception { } @Test public void ä¸è¬ã¦ã¼ã¶æ¨©éãæã¤ã¦ã¼ã¶ã¯è²¸åºå¸ææ¸ç±CSVãã¡ã¤ã«ã¢ãããã¼ãç»é¢ã表示ã§ãã() throws Exception { } @Test public void ãã°ã¤ã³ãã¦ããªããã°ã¢ãããã¼ãç¨ã®URLã¯å¼ã³åºããªã() throws Exception { } @Test public void ãã°ã¤ã³ãã¦ããªããã°ç¢ºèªç»é¢ã®ç»é²ãã¿ã³ç¨ã®URLã¯å¼ã³åºããªã() throws Exception { } } @RunWith(SpringJUnit4ClassRunner.class) @SpringApplicationConfiguration(classes = Application.class) @WebAppConfiguration public static class ãã¡ã¤ã«ã¢ãããã¼ãã®ãã¹ã_ã¨ã©ã¼ã«ãªãå ´å { @Test public void ã¨ã©ã¼ãåºãCSVãã¡ã¤ã«ãã¢ãããã¼ãããã¨ã¢ãããã¼ãç»é¢ã«ã¨ã©ã¼ã¡ãã»ã¼ã¸ã表示ãã() throws Exception { } } @RunWith(SpringJUnit4ClassRunner.class) @SpringApplicationConfiguration(classes = Application.class) @WebAppConfiguration public static class ãã¡ã¤ã«ã¢ãããã¼ãã®ãã¹ã_æ£å¸¸ãªå ´å { @Test public void ã¨ã©ã¼ã®ãªãCSVãã¡ã¤ã«ãã¢ãããã¼ããã¦ãã確èªç»é¢ã§ç»é²ãã¿ã³ãã¯ãªãã¯ããã¨å®äºç»é¢ã表示ããã() throws Exception { } } }
â ãã®ï¼
package ksbysample.webapp.lending.web.booklist; import ksbysample.common.test.SecurityMockMvcResource; import ksbysample.common.test.TestDataResource; import ksbysample.webapp.lending.Application; import ksbysample.webapp.lending.service.file.BooklistCsvFileServiceTest; 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.mock.web.MockMultipartFile; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import org.springframework.test.context.web.WebAppConfiguration; import org.springframework.test.web.servlet.MvcResult; import static ksbysample.common.test.ErrorsResultMatchers.errors; import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.csrf; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*; @RunWith(Enclosed.class) public class BooklistControllerTest { @RunWith(SpringJUnit4ClassRunner.class) @SpringApplicationConfiguration(classes = Application.class) @WebAppConfiguration public static class ã¦ã¼ã¶æ¨©éã¨URLã®å¼ã³åºãå¯å¦ã®ãã¹ã { @Rule @Autowired public TestDataResource testDataResource; @Rule @Autowired public SecurityMockMvcResource mvc; @Test public void ãã°ã¤ã³ãã¦ããªããã°è²¸åºå¸ææ¸ç±CSVãã¡ã¤ã«ã¢ãããã¼ãç»é¢ã¯è¡¨ç¤ºã§ããªã() throws Exception { mvc.noauth.perform(get("/booklist")) .andExpect(status().isFound()) .andExpect(redirectedUrl("http://localhost/")); } @Test public void ä¸è¬ã¦ã¼ã¶æ¨©éãæã¤ã¦ã¼ã¶ã¯è²¸åºå¸ææ¸ç±CSVãã¡ã¤ã«ã¢ãããã¼ãç»é¢ã表示ã§ãã() throws Exception { mvc.authSuzukiHanako.perform(get("/booklist")) .andExpect(status().isOk()) .andExpect(view().name("booklist/booklist")); } @Test public void ãã°ã¤ã³ãã¦ããªããã°ã¢ãããã¼ãç¨ã®URLã¯å¼ã³åºããªã() throws Exception { mvc.noauth.perform(post("/booklist/fileupload")) .andExpect(status().isForbidden()); } @Test public void ãã°ã¤ã³ãã¦ããªããã°ç¢ºèªç»é¢ã®ç»é²ãã¿ã³ç¨ã®URLã¯å¼ã³åºããªã() throws Exception { mvc.noauth.perform(post("/booklist/register")) .andExpect(status().isForbidden()); } } @RunWith(SpringJUnit4ClassRunner.class) @SpringApplicationConfiguration(classes = Application.class) @WebAppConfiguration public static class ãã¡ã¤ã«ã¢ãããã¼ãã®ãã¹ã_ã¨ã©ã¼ã«ãªãå ´å { @Rule @Autowired public TestDataResource testDataResource; @Rule @Autowired public SecurityMockMvcResource mvc; @Test public void ã¨ã©ã¼ãåºãCSVãã¡ã¤ã«ãã¢ãããã¼ãããã¨ã¢ãããã¼ãç»é¢ã«ã¨ã©ã¼ã¡ãã»ã¼ã¸ã表示ãã() throws Exception { BooklistCsvFileServiceTest booklistCsvFileServiceTest = new BooklistCsvFileServiceTest(); MockMultipartFile multipartFile = booklistCsvFileServiceTest.createErrorCsvFile(); mvc.authSuzukiHanako.perform( fileUpload("/booklist/fileupload") // åã« .file(multipartFile) ã§ã¯ãã¡ã§ã // 第ï¼å¼æ°ã« <input type="file" name="..."> ã® name å±æ§ã®æååãã第ï¼å¼æ°ã«ãã¡ã¤ã«ã®ãã¤ãé åã渡ã .file("fileupload", multipartFile.getBytes()) .with(csrf())) .andExpect(status().isOk()) .andExpect(view().name("booklist/booklist")) .andExpect(model().hasErrors()) .andExpect(model().errorCount(6)) .andExpect(errors().hasGlobalError("uploadBooklistForm", "UploadBooklistForm.fileupload.lengtherr")) .andExpect(errors().hasGlobalError("uploadBooklistForm", "UploadBooklistForm.fileupload.isbn.patternerr")) .andExpect(errors().hasGlobalError("uploadBooklistForm", "UploadBooklistForm.fileupload.isbn.lengtherr")) .andExpect(errors().hasGlobalError("uploadBooklistForm", "UploadBooklistForm.fileupload.isbn.numlengtherr")) .andExpect(errors().hasGlobalError("uploadBooklistForm", "UploadBooklistForm.fileupload.isbn.numlengtherr")) .andExpect(errors().hasGlobalError("uploadBooklistForm", "UploadBooklistForm.fileupload.bookname.lengtherr")); } } @RunWith(SpringJUnit4ClassRunner.class) @SpringApplicationConfiguration(classes = Application.class) @WebAppConfiguration public static class ãã¡ã¤ã«ã¢ãããã¼ãã®ãã¹ã_æ£å¸¸ãªå ´å { @Rule @Autowired public TestDataResource testDataResource; @Rule @Autowired public SecurityMockMvcResource mvc; @Test public void ã¨ã©ã¼ã®ãªãCSVãã¡ã¤ã«ãã¢ãããã¼ããã¦ãã確èªç»é¢ã§ç»é²ãã¿ã³ãã¯ãªãã¯ããã¨å®äºç»é¢ã表示ããã() throws Exception { BooklistCsvFileServiceTest booklistCsvFileServiceTest = new BooklistCsvFileServiceTest(); MockMultipartFile multipartFile = booklistCsvFileServiceTest.createNoErrorCsvFile(); // CSVãã¡ã¤ã«ãã¢ãããã¼ããã MvcResult result = mvc.authSuzukiHanako.perform( fileUpload("/booklist/fileupload") // åã« .file(multipartFile) ã§ã¯ãã¡ã§ã // 第ï¼å¼æ°ã« <input type="file" name="..."> ã® name å±æ§ã®æååãã第ï¼å¼æ°ã«ãã¡ã¤ã«ã®ãã¤ãé åã渡ã .file("fileupload", multipartFile.getBytes()) .with(csrf())) .andExpect(status().isOk()) .andExpect(view().name("booklist/fileupload")) .andExpect(model().hasNoErrors()) .andReturn(); RegisterBooklistForm registerBooklistForm = (RegisterBooklistForm) result.getModelAndView().getModel().get("registerBooklistForm"); // 確èªç»é¢ã§ç»é²ãã¿ã³ãã¯ãªãã¯ãã mvc.authSuzukiHanako.perform( post("/booklist/register") .param("lendingAppId", registerBooklistForm.getLendingAppId().toString()) .with(csrf())) .andExpect(status().isFound()) .andExpect(redirectedUrl("/booklist/complete")); // å®äºç»é¢ã表示ãã mvc.authSuzukiHanako.perform(get("/booklist/complete")) .andExpect(status().isOk()) .andExpect(view().name("booklist/complete")); } } }
OpenWeatherMapApiServiceTest.java
@Ignore("ã¢ã¯ã»ã¹ãã¼ãè¦æ±ãããããã«ãªã£ãã®ã§ãã¹ããå®è¡å¯¾è±¡å¤ã«ãã") @RunWith(SpringJUnit4ClassRunner.class) @SpringApplicationConfiguration(classes = Application.class) @WebAppConfiguration public class OpenWeatherMapApiServiceTest {
- ã¯ã©ã¹ã«
@Ignore("ã¢ã¯ã»ã¹ãã¼ãè¦æ±ãããããã«ãªã£ãã®ã§ãã¹ããå®è¡å¯¾è±¡å¤ã«ãã")
ãä»å ãã¾ãã
å±¥æ´
2015/10/12
åççºè¡ã