|
| 1 | +package com.didispace; |
| 2 | + |
| 3 | +import com.didispace.web.HelloController; |
| 4 | +import org.junit.Before; |
| 5 | +import org.junit.Test; |
| 6 | +import org.junit.runner.RunWith; |
| 7 | +import org.springframework.boot.test.SpringApplicationConfiguration; |
| 8 | +import org.springframework.http.MediaType; |
| 9 | +import org.springframework.mock.web.MockServletContext; |
| 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 | + |
| 20 | + |
| 21 | +/** |
| 22 | + * |
| 23 | + * @author 程序猿DD |
| 24 | + * @version 1.0.0 |
| 25 | + * @blog http://blog.didispace.com |
| 26 | + * |
| 27 | + */ |
| 28 | +@RunWith(SpringJUnit4ClassRunner.class) |
| 29 | +@SpringApplicationConfiguration(classes = MockServletContext.class) |
| 30 | +@WebAppConfiguration |
| 31 | +public class ApplicationTests { |
| 32 | + |
| 33 | + private MockMvc mvc; |
| 34 | + |
| 35 | + @Before |
| 36 | + public void setUp() throws Exception { |
| 37 | + mvc = MockMvcBuilders.standaloneSetup( |
| 38 | + new HelloController()).build(); |
| 39 | + } |
| 40 | + |
| 41 | + @Test |
| 42 | + public void getHello() throws Exception { |
| 43 | + mvc.perform(MockMvcRequestBuilders.get("/hello").accept(MediaType.APPLICATION_JSON)) |
| 44 | + .andExpect(status().isOk()) |
| 45 | + .andExpect(content().string(equalTo("Hello World"))); |
| 46 | + } |
| 47 | + |
| 48 | +} |
0 commit comments