|
1 | 1 | package com.didispace;
|
2 | 2 |
|
| 3 | +import com.didispace.web.HelloController; |
| 4 | +import org.junit.Before; |
3 | 5 | import org.junit.Test;
|
4 | 6 | import org.junit.runner.RunWith;
|
5 | 7 | import org.springframework.boot.test.SpringApplicationConfiguration;
|
| 8 | +import org.springframework.http.MediaType; |
| 9 | +import org.springframework.mock.web.MockServletContext; |
6 | 10 | import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
| 11 | +import org.springframework.test.context.web.WebAppConfiguration; |
| 12 | +import org.springframework.test.web.servlet.MockMvc; |
| 13 | +import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; |
| 14 | +import org.springframework.test.web.servlet.setup.MockMvcBuilders; |
| 15 | + |
| 16 | +import static org.hamcrest.Matchers.equalTo; |
| 17 | +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content; |
| 18 | +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; |
| 19 | + |
7 | 20 |
|
8 | 21 | @RunWith(SpringJUnit4ClassRunner.class)
|
9 |
| -@SpringApplicationConfiguration(classes = Chapter1Application.class) |
| 22 | +@SpringApplicationConfiguration(classes = MockServletContext.class) |
| 23 | +@WebAppConfiguration |
10 | 24 | public class Chapter1ApplicationTests {
|
11 | 25 |
|
| 26 | + private MockMvc mvc; |
| 27 | + |
| 28 | + @Before |
| 29 | + public void setUp() throws Exception { |
| 30 | + mvc = MockMvcBuilders.standaloneSetup(new HelloController()).build(); |
| 31 | + } |
| 32 | + |
12 | 33 | @Test
|
13 |
| - public void contextLoads() { |
| 34 | + public void getHello() throws Exception { |
| 35 | + mvc.perform(MockMvcRequestBuilders.get("/hello").accept(MediaType.APPLICATION_JSON)) |
| 36 | + .andExpect(status().isOk()) |
| 37 | + .andExpect(content().string(equalTo("Hello World"))); |
14 | 38 | }
|
15 | 39 |
|
16 | 40 | }
|
0 commit comments