From cd6fa32da8838b75f91526a6b26a0c08c17fd1f8 Mon Sep 17 00:00:00 2001 From: bwh Date: Thu, 11 Apr 2019 00:04:07 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E5=96=84SpringBoot=20=20Hello=20World?= =?UTF-8?q?=20=E5=8D=95=E5=85=83=E6=B5=8B=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/main/resources/application.properties | 1 + .../QuickStartApplicationTests.java | 30 ++++++++++++++++--- .../src/test/resources/application.properties | 1 + 3 files changed, 28 insertions(+), 4 deletions(-) create mode 100644 chapter-1-spring-boot-quickstart/src/test/resources/application.properties diff --git a/chapter-1-spring-boot-quickstart/src/main/resources/application.properties b/chapter-1-spring-boot-quickstart/src/main/resources/application.properties index e69de29..aba2a53 100644 --- a/chapter-1-spring-boot-quickstart/src/main/resources/application.properties +++ b/chapter-1-spring-boot-quickstart/src/main/resources/application.properties @@ -0,0 +1 @@ +server.port=-1 \ No newline at end of file diff --git a/chapter-1-spring-boot-quickstart/src/test/java/demo/springboot/QuickStartApplicationTests.java b/chapter-1-spring-boot-quickstart/src/test/java/demo/springboot/QuickStartApplicationTests.java index 5220ed5..124f83b 100644 --- a/chapter-1-spring-boot-quickstart/src/test/java/demo/springboot/QuickStartApplicationTests.java +++ b/chapter-1-spring-boot-quickstart/src/test/java/demo/springboot/QuickStartApplicationTests.java @@ -1,16 +1,38 @@ package demo.springboot; + import org.junit.Test; import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.http.MediaType; +import org.springframework.test.context.TestPropertySource; import org.springframework.test.context.junit4.SpringRunner; +import org.springframework.test.web.servlet.MockMvc; + +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; @RunWith(SpringRunner.class) -@SpringBootTest +@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, classes = QuickStartApplication.class) +@AutoConfigureMockMvc +@TestPropertySource(locations = "classpath:application.properties") public class QuickStartApplicationTests { - @Test - public void contextLoads() { - } + @Autowired + private MockMvc mvc; + + + @Test + public void requestHello_thenStatus200_and_outputHello() throws Exception { + mvc.perform(get("/hello") + .contentType(MediaType.APPLICATION_JSON)) + .andExpect(status().isOk()) + .andExpect(content().contentTypeCompatibleWith(MediaType.TEXT_PLAIN)) + .andExpect(content().encoding("UTF-8")) + .andExpect(content().string("Hello,Spring Boot!")); + } } diff --git a/chapter-1-spring-boot-quickstart/src/test/resources/application.properties b/chapter-1-spring-boot-quickstart/src/test/resources/application.properties new file mode 100644 index 0000000..aba2a53 --- /dev/null +++ b/chapter-1-spring-boot-quickstart/src/test/resources/application.properties @@ -0,0 +1 @@ +server.port=-1 \ No newline at end of file