@RunWith(SpringRunner.class) @SpringBootTest @WebAppConfiguration @AutoConfigureMockMvc public class VideoTest { //注入MockMvc @Autowired private MockMvc mockMvc; @Test public void testVideoListApi() throws Exception{ //传入一个requestBuilder MockMvcRequestBuilders:构建器 构建http请求 //发出请求后 判断响应码 MvcResult mvcResult = mockMvc.perform(MockMvcRequestBuilders.get("/api/v1/pub/vide/list")) .andExpect(MockMvcResultMatchers.status().isOk()).andReturn();//判断是否是http200响应码 //拿到响应结果 int status = mvcResult.getResponse().getStatus(); //返回结果实体 System.out.println(status); //会乱码 //String result = mvcResult.getResponse().getContentAsString(); // 使用下面这个,增加 编码 说明,就不会乱码打印 String result = mvcResult.getResponse().getContentAsString(Charset.forName("utf-8")); System.out.println(result); } } @RestController @RequestMapping("user") public class UserController { @RequestMapping("getUser") public User getUser(){ User user = new User(); user.setId(2); user.setUsername("dds"); user.setUsername("qqqq"); System.out.println("aaa"); return user; } } public class User { private int id; private String username; private String password; public User(int id, String username, String password) { this.id = id; this.username = username; this.password = password; } public User() { } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public int getId() { return id; } public void setId(int id) { this.id = id; } @Override public String toString() { return "User{" + "id=" + id + ", username='" + username + '\'' + ", password='" + password + '\'' + '}'; } }