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"))); } }