1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
| @AutoConfigureMockMvc @RunWith(SpringRunner.class) @SpringBootTest(classes = Application.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) public class ApplicationTests { @Autowired private MockMvc mvc;
@Test public void testUserController() throws Exception { RequestBuilder request;
request = get("/users/"); MvcResult result = mvc.perform(request) .andExpect(status().isOk()) .andReturn(); String content = result.getResponse().getContentAsString(); BaseResponse<List<User>> response = JacksonUtil.json2Bean(content, new TypeReference<BaseResponse<List<User>>>() {}); assertThat(response.isSuccess(), is(true)); assertThat(response.getMsg(), is("查询列表成功")); assertThat(((List) response.getData()).size(), is(0));
request = post("/users/") .param("id", "1") .param("name", "测试大师") .param("age", "20"); result = mvc.perform(request) .andExpect(status().isOk()) .andReturn(); content = result.getResponse().getContentAsString(); BaseResponse<String> response1 = JacksonUtil.json2Bean(content, new TypeReference<BaseResponse<String>>() {}); assertThat(response1.isSuccess(), is(true)); assertThat(response1.getMsg(), is("新增成功"));
request = get("/users/"); result = mvc.perform(request) .andExpect(status().isOk()) .andReturn(); content = result.getResponse().getContentAsString(); BaseResponse<List<User>> response2 = JacksonUtil.json2Bean(content, new TypeReference<BaseResponse<List<User>>>() {}); assertThat(response2.isSuccess(), is(true)); assertThat(response2.getMsg(), is("查询列表成功")); assertThat((response2.getData()).size(), is(1));
request = put("/users/1") .param("name", "测试终极大师") .param("age", "30"); result = mvc.perform(request) .andExpect(status().isOk()) .andReturn(); content = result.getResponse().getContentAsString(); BaseResponse<String> response3 = JacksonUtil.json2Bean(content, new TypeReference<BaseResponse<String>>() {}); assertThat(response3.isSuccess(), is(true)); assertThat(response3.getMsg(), is("更新成功"));
request = get("/users/1"); result = mvc.perform(request) .andExpect(status().isOk()) .andReturn(); content = result.getResponse().getContentAsString(); BaseResponse<User> response4 = JacksonUtil.json2Bean(content, new TypeReference<BaseResponse<User>>() {}); assertThat(response4.isSuccess(), is(true)); assertThat(response4.getMsg(), is("查询成功")); User user = response4.getData(); assertThat(user.getId(), is(1L)); assertThat(user.getName(), is("测试终极大师"));
request = delete("/users/1"); result = mvc.perform(request) .andExpect(status().isOk()) .andReturn(); content = result.getResponse().getContentAsString(); BaseResponse<String> response5 = JacksonUtil.json2Bean(content, new TypeReference<BaseResponse<String>>() {}); assertThat(response5.isSuccess(), is(true)); assertThat(response5.getMsg(), is("删除成功"));
request = get("/users/"); result = mvc.perform(request) .andExpect(status().isOk()) .andReturn(); content = result.getResponse().getContentAsString(); BaseResponse<List<User>> response6 = JacksonUtil.json2Bean(content, new TypeReference<BaseResponse<List<User>>>() {}); assertThat(response6.isSuccess(), is(true)); assertThat(response6.getMsg(), is("查询列表成功")); assertThat((response6.getData()).size(), is(0)); }
}
|