å ±æã©ã¤ãã©ãªã管çããããã« Sonatype ã® Nexus Repository Manager OSS ã使ç¨ãã ( ãã®ï¼ï¼ )( Spring Framework ã«ä¾åããã©ã¤ãã©ãªãä½æããï¼ )
æ¦è¦
- ä»åã®æé ã§ç¢ºèªã§ããã®ã¯ä»¥ä¸ã®å
容ã§ãã
- ãã¹ãã®å®è£ ãåã³ Nexus ã¸ã®ç»é²ã¾ã§é²ãã¾ãã
åç §ãããµã¤ãã»æ¸ç±
-
Spring Boot Reference Guide - 40. Testing - 40.4.3 OutputCapture
https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-testing.html#boot-features-output-capture-test-utility- æ¨æºåºåã«åºåãããå 容ã OutputCapture ã§ç¢ºèªããæ¹æ³ãåç §ãã¾ããã
-
Spring Framework Reference Documentation - 15. Integration Testing - @TestPropertySource
http://docs.spring.io/spring/docs/current/spring-framework-reference/html/integration-testing.html#__testpropertysource@TestPropertySource
ã使ç¨ãã¦ããã¹ãã¯ã©ã¹ã§ä¸æçã« properties ãè¨å®ããæ¹æ³ãåç §ãã¾ããã
ç®æ¬¡
- src/test/java ã®ä¸ã«ãã¹ãç¨ã® main class ãä½æãã
- ãã¹ãç¨ã® Controller class ãä½æãã
- RequestAndResponseLoggerAutoConfiguration ã¯ã©ã¹ã®ãã¹ããä½æãã
- å ¨ã¦ã®ãã¹ããæåããã確èªãã
- Nexus ã«ç»é²ãã
æé
src/test/java ã®ä¸ã«ãã¹ãç¨ã® main class ãä½æãã
main class ã¯ãã¹ãç¨ã«ããã°ããã®ã§ãsrc/main/java ã®ä¸ã§ã¯ãªã src/test/java ã®ä¸ã«ä½æãã¾ãã
src/test/java ã®ä¸ã« ksbysample.library.dependspring ããã±ã¼ã¸ãä½æãã¾ãã
src/test/java/ksbysample/library/dependspring ã®ä¸ã« Application.java ãä½æãããªã³ã¯å ã®å 容 ãè¨è¿°ãã¾ãã
ãã¹ãç¨ã® Controller class ãä½æãã
- src/test/java/ksbysample/library/dependspring ã®ä¸ã« TestController.java ãä½æãããªã³ã¯å ã®å 容 ãè¨è¿°ãã¾ãã
RequestAndResponseLoggerAutoConfiguration ã¯ã©ã¹ã®ãã¹ããä½æãã
src/main/java/ksbysample/library/dependspring/config ã®ä¸ã® RequestAndResponseLoggerAutoConfiguration.java ã§ãCreate Testããã¤ã¢ãã°ã表示ãããã¹ãã¯ã©ã¹ãä½æãã¾ãã
ãChoose Destination Directoryããã¤ã¢ãã°ã表示ãããã src/test/java ã®æ¹ãé¸æãã¦ãOKããã¿ã³ãã¯ãªãã¯ãã¾ãã
src/test/java/ksbysample/library/dependspring/config ã®ä¸ã« RequestAndResponseLoggerAutoConfigurationTest.java ãä½æããã¾ãã®ã§ããªã³ã¯å ã®å 容 ãè¨è¿°ãã¾ãã
å ¨ã¦ã®ãã¹ããæåããã確èªãã
ãã¹ããå®è¡ãã¾ããRequestAndResponseLoggerAutoConfigurationTest ã¯ã©ã¹ã®ã¯ã©ã¹åã®å·¦å´ã«è¡¨ç¤ºããã¦ããã¢ã¤ã³ã³ãã¯ãªãã¯ãã¦ã³ã³ããã¹ãã¡ãã¥ã¼ã表示å¾ãRun 'RequestAndResponseLoggerAutoCo...'ããé¸æãã¾ãã
ãã¹ããæåãããã¨ã確èªã§ãã¾ãã
ããã°ãåºåããããå ´åã®ãã¹ãã®ãã°ãè¦ã㨠request, response ã®ãã¼ã¿ãåºåããã¦ãããã¨ã確èªã§ãã¾ãã
Nexus ã«ç»é²ãã
Gradle projects View ãã uploadArchives ã¿ã¹ã¯ãå®è¡ãã¾ããBUILD SUCCESSFUL ã表示ããã¦æ£å¸¸ã« Nexus ã«ç»é²ããã¾ããã
Nexus ã®ç®¡çç»é¢ãè¦ã㨠maven-releases repository ã«ç»é²ããã¦ãããã¨ã確èªã§ãã¾ãã
ksbysample-library-depend-spring-1.0.0-RELEASE.pom ãè¦ã㨠ãªã³ã¯å ã®å 容 ã§ç»é²ããã¦ãã¾ããã
以ä¸ã®å ´æã® .gitkeep ãåé¤ããå¾ãcommit ãã¾ãã
ã½ã¼ã¹ã³ã¼ã
Application.java
package ksbysample.library.dependspring; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } }
TestController.java
package ksbysample.library.dependspring; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; @Controller @RequestMapping("/test") public class TestController { @RequestMapping @ResponseBody public String index() { return "test"; } }
RequestAndResponseLoggerAutoConfigurationTest.java
package ksbysample.library.dependspring.config; import ksbysample.library.dependspring.Application; import org.junit.Before; 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.OutputCapture; import org.springframework.boot.test.SpringApplicationConfiguration; import org.springframework.test.context.TestPropertySource; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import org.springframework.test.context.web.WebAppConfiguration; import org.springframework.test.web.servlet.MockMvc; import org.springframework.test.web.servlet.setup.MockMvcBuilders; import org.springframework.web.context.WebApplicationContext; import static org.assertj.core.api.Assertions.assertThat; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; @RunWith(Enclosed.class) public class RequestAndResponseLoggerAutoConfigurationTest { @RunWith(SpringJUnit4ClassRunner.class) @SpringApplicationConfiguration(classes = Application.class) @WebAppConfiguration public static class ããã©ã«ãã®è¨å®ã§ã®ãã¹ã { public MockMvc mvc; @Autowired private WebApplicationContext context; @Rule public OutputCapture capture = new OutputCapture(); @Before public void setUp() throws Exception { this.mvc = MockMvcBuilders.webAppContextSetup(this.context) .build(); } @Test public void ãã°ãåºåããã() throws Exception { mvc.perform(get("/test")) .andExpect(status().isOk()); assertThat(capture.toString()).contains("[req][info ] REQUEST_URI = /test"); assertThat(capture.toString()).contains("[res][info ] RESPONSE_STATUS = 200"); } } @RunWith(SpringJUnit4ClassRunner.class) @SpringApplicationConfiguration(classes = Application.class) @WebAppConfiguration @TestPropertySource(properties = {"ksbysample.library.request-and-response-logger.execution=execution(* ksbysample.library.dependspring..*.*(..))"}) public static class executionããããã£è¨å®æã®ãã¹ã_åºåãããè¨å® { public MockMvc mvc; @Autowired private WebApplicationContext context; @Rule public OutputCapture capture = new OutputCapture(); @Before public void setUp() throws Exception { this.mvc = MockMvcBuilders.webAppContextSetup(this.context) .build(); } @Test public void ãã°ãåºåããã() throws Exception { mvc.perform(get("/test")) .andExpect(status().isOk()); assertThat(capture.toString()).contains("[req][info ] REQUEST_URI = /test"); assertThat(capture.toString()).contains("[res][info ] RESPONSE_STATUS = 200"); } } @RunWith(SpringJUnit4ClassRunner.class) @SpringApplicationConfiguration(classes = Application.class) @WebAppConfiguration @TestPropertySource(properties = {"ksbysample.library.request-and-response-logger.execution=execution(* ksbysample.webapp.lending.web..*.*(..))"}) public static class executionããããã£è¨å®æã®ãã¹ã_åºåãããªãè¨å® { public MockMvc mvc; @Autowired private WebApplicationContext context; @Rule public OutputCapture capture = new OutputCapture(); @Before public void setUp() throws Exception { this.mvc = MockMvcBuilders.webAppContextSetup(this.context) .build(); } @Test public void ãã°ãåºåãããªã() throws Exception { mvc.perform(get("/test")) .andExpect(status().isOk()); assertThat(capture.toString()).doesNotContain("[req][info ] REQUEST_URI = /test"); assertThat(capture.toString()).doesNotContain("[res][info ] RESPONSE_STATUS = 200"); } } }
ksbysample-library-depend-spring-1.0.0-RELEASE.pom
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>ksbysample.library</groupId> <artifactId>ksbysample-library-depend-spring</artifactId> <version>1.0.0-RELEASE</version> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-autoconfigure</artifactId> <scope>compile</scope> <exclusions> <exclusion> <artifactId>commons-logging</artifactId> <groupId>commons-logging</groupId> </exclusion> </exclusions> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <scope>compile</scope> <exclusions> <exclusion> <artifactId>commons-logging</artifactId> <groupId>commons-logging</groupId> </exclusion> </exclusions> </dependency> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> <scope>compile</scope> <exclusions> <exclusion> <artifactId>commons-logging</artifactId> <groupId>commons-logging</groupId> </exclusion> </exclusions> </dependency> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-lang3</artifactId> <version>3.5</version> <scope>compile</scope> <exclusions> <exclusion> <artifactId>commons-logging</artifactId> <groupId>commons-logging</groupId> </exclusion> </exclusions> </dependency> <dependency> <groupId>com.google.guava</groupId> <artifactId>guava</artifactId> <version>20.0</version> <scope>compile</scope> <exclusions> <exclusion> <artifactId>commons-logging</artifactId> <groupId>commons-logging</groupId> </exclusion> </exclusions> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.assertj</groupId> <artifactId>assertj-core</artifactId> <version>3.6.1</version> <scope>test</scope> </dependency> </dependencies> <name>Springä¾åã®ãã¹ãã©ã¤ãã©ãª ksbysample-library-depend-spring</name> <description>Spring ã«ä¾åããå ±æã©ã¤ãã©ãªã§ãã</description> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.3.8.RELEASE</version> <scope>import</scope> <type>pom</type> </dependency> <dependency> <groupId>io.spring.platform</groupId> <artifactId>platform-bom</artifactId> <version>2.0.8.RELEASE</version> <scope>import</scope> <type>pom</type> </dependency> </dependencies> </dependencyManagement> </project>
å±¥æ´
2016/12/26
åççºè¡ã