From 7c3bf312611aa03d122baaa0deb6f5bddbc307ae Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=A8=8B=E5=BA=8F=E7=8C=BFDD?= Date: Tue, 23 Feb 2016 16:51:34 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0=E6=A0=B7=E4=BE=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Chapter1/pom.xml | 2 +- .../com/didispace/web/HelloController.java | 14 ++++++++++ .../didispace/Chapter1ApplicationTests.java | 28 +++++++++++++++++-- 3 files changed, 41 insertions(+), 3 deletions(-) create mode 100644 Chapter1/src/main/java/com/didispace/web/HelloController.java diff --git a/Chapter1/pom.xml b/Chapter1/pom.xml index 1d18e87..cc4e377 100644 --- a/Chapter1/pom.xml +++ b/Chapter1/pom.xml @@ -5,7 +5,7 @@ com.didispace Chapter1 - 0.0.1-SNAPSHOT + 1.0.0 jar Chapter1 diff --git a/Chapter1/src/main/java/com/didispace/web/HelloController.java b/Chapter1/src/main/java/com/didispace/web/HelloController.java new file mode 100644 index 0000000..9455485 --- /dev/null +++ b/Chapter1/src/main/java/com/didispace/web/HelloController.java @@ -0,0 +1,14 @@ +package com.didispace.web; + +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +@RestController +public class HelloController { + + @RequestMapping("/hello") + public String index() { + return "Hello World"; + } + +} \ No newline at end of file diff --git a/Chapter1/src/test/java/com/didispace/Chapter1ApplicationTests.java b/Chapter1/src/test/java/com/didispace/Chapter1ApplicationTests.java index 1180149..0b5bfb0 100644 --- a/Chapter1/src/test/java/com/didispace/Chapter1ApplicationTests.java +++ b/Chapter1/src/test/java/com/didispace/Chapter1ApplicationTests.java @@ -1,16 +1,40 @@ package com.didispace; +import com.didispace.web.HelloController; +import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.boot.test.SpringApplicationConfiguration; +import org.springframework.http.MediaType; +import org.springframework.mock.web.MockServletContext; 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.request.MockMvcRequestBuilders; +import org.springframework.test.web.servlet.setup.MockMvcBuilders; + +import static org.hamcrest.Matchers.equalTo; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; + @RunWith(SpringJUnit4ClassRunner.class) -@SpringApplicationConfiguration(classes = Chapter1Application.class) +@SpringApplicationConfiguration(classes = MockServletContext.class) +@WebAppConfiguration public class Chapter1ApplicationTests { + private MockMvc mvc; + + @Before + public void setUp() throws Exception { + mvc = MockMvcBuilders.standaloneSetup(new HelloController()).build(); + } + @Test - public void contextLoads() { + public void getHello() throws Exception { + mvc.perform(MockMvcRequestBuilders.get("/hello").accept(MediaType.APPLICATION_JSON)) + .andExpect(status().isOk()) + .andExpect(content().string(equalTo("Hello World"))); } }