|
1 | 1 | package com.springboot.springbootquickstart;
|
2 | 2 |
|
| 3 | +import com.springboot.springbootquickstart.controller.UserController; |
| 4 | +import org.junit.Before; |
3 | 5 | import org.junit.Test;
|
4 | 6 | import org.junit.runner.RunWith;
|
| 7 | +import org.springframework.beans.factory.annotation.Autowired; |
5 | 8 | import org.springframework.boot.test.context.SpringBootTest;
|
| 9 | +import org.springframework.http.MediaType; |
6 | 10 | import org.springframework.test.context.junit4.SpringRunner;
|
| 11 | +import org.springframework.test.web.servlet.MockMvc; |
| 12 | +import org.springframework.test.web.servlet.MvcResult; |
| 13 | +import org.springframework.test.web.servlet.RequestBuilder; |
| 14 | +import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; |
| 15 | +import org.springframework.test.web.servlet.result.MockMvcResultHandlers; |
| 16 | +import org.springframework.test.web.servlet.result.MockMvcResultMatchers; |
| 17 | +import org.springframework.test.web.servlet.setup.MockMvcBuilders; |
| 18 | +import org.springframework.web.context.WebApplicationContext; |
7 | 19 |
|
8 | 20 | @RunWith(SpringRunner.class)
|
9 | 21 | @SpringBootTest
|
10 | 22 | public class SpringBootQuickStartApplicationTests {
|
11 | 23 |
|
| 24 | + private MockMvc mvc; |
| 25 | + |
| 26 | + @Before |
| 27 | + public void setUp() throws Exception { |
| 28 | + mvc = MockMvcBuilders.standaloneSetup(new UserController()).build(); |
| 29 | + } |
| 30 | + |
12 | 31 | @Test
|
13 |
| - public void contextLoads() { |
| 32 | + public void contextLoads() throws Exception { |
| 33 | + RequestBuilder request = null; |
| 34 | + |
| 35 | + // 1、get查一下user列表,应该为空 |
| 36 | + request = MockMvcRequestBuilders.get("/") |
| 37 | + .contentType(MediaType.APPLICATION_JSON); |
| 38 | + mvc.perform(request) |
| 39 | + .andExpect(MockMvcResultMatchers.status().isOk()) |
| 40 | + .andDo(MockMvcResultHandlers.print()) |
| 41 | + .andReturn(); |
| 42 | + |
| 43 | + // 2、post提交一个user |
| 44 | + request = MockMvcRequestBuilders.post("/") |
| 45 | + .param("id", "1") |
| 46 | + .param("name", "Spring Boot") |
| 47 | + .param("age", "18") |
| 48 | + .contentType(MediaType.APPLICATION_JSON); |
| 49 | + mvc.perform(request) |
| 50 | + .andExpect(MockMvcResultMatchers.status().isOk()) |
| 51 | + .andDo(MockMvcResultHandlers.print()) |
| 52 | + .andReturn(); |
| 53 | + |
| 54 | + |
| 55 | + // 3、get获取user列表,应该有刚才插入的数据 |
| 56 | + request = MockMvcRequestBuilders.get("/") |
| 57 | + .contentType(MediaType.APPLICATION_JSON); |
| 58 | + mvc.perform(request) |
| 59 | + .andExpect(MockMvcResultMatchers.status().isOk()) |
| 60 | + .andDo(MockMvcResultHandlers.print()) |
| 61 | + .andReturn(); |
| 62 | + |
| 63 | + // 4、put修改id为1的user |
| 64 | + request = MockMvcRequestBuilders.put("/1") |
| 65 | + .param("name", "Spring Boot Test") |
| 66 | + .contentType(MediaType.APPLICATION_JSON); |
| 67 | + |
| 68 | + mvc.perform(request) |
| 69 | + .andExpect(MockMvcResultMatchers.status().isOk()) |
| 70 | + .andDo(MockMvcResultHandlers.print()) |
| 71 | + .andReturn(); |
| 72 | + |
| 73 | + // 5、get一个id为1的user |
| 74 | + request = MockMvcRequestBuilders.get("/1") |
| 75 | + .contentType(MediaType.APPLICATION_JSON); |
| 76 | + |
| 77 | + mvc.perform(request) |
| 78 | + .andExpect(MockMvcResultMatchers.status().isOk()) |
| 79 | + .andDo(MockMvcResultHandlers.print()) |
| 80 | + .andReturn(); |
| 81 | + |
| 82 | + // 6、del删除id为1的user |
| 83 | + request = MockMvcRequestBuilders.delete("/1") |
| 84 | + .contentType(MediaType.APPLICATION_JSON); |
| 85 | + |
| 86 | + mvc.perform(request) |
| 87 | + .andExpect(MockMvcResultMatchers.status().isOk()) |
| 88 | + .andDo(MockMvcResultHandlers.print()) |
| 89 | + .andReturn(); |
| 90 | + |
| 91 | + // 7、get查一下user列表,应该为空 |
| 92 | + |
| 93 | + request = MockMvcRequestBuilders.get("/") |
| 94 | + .contentType(MediaType.APPLICATION_JSON); |
| 95 | + |
| 96 | + mvc.perform(request) |
| 97 | + .andExpect(MockMvcResultMatchers.status().isOk()) |
| 98 | + .andDo(MockMvcResultHandlers.print()) |
| 99 | + .andReturn(); |
| 100 | + |
14 | 101 | }
|
15 | 102 |
|
16 | 103 | }
|
0 commit comments