步骤
1.添加注解
@AutoConfigureMockMvc
2.添加MockMvc类
@Autowired
private MockMvc mockMvc;
3.填加需要操作的接口(其他用法 只需要改变其中get(“xxxxxx”)即可)
MvcResult mvcResult= mockMvc.perform(MockMvcRequestBuilders.get(“/api/v1/pub/video/list”))
.andExpect(MockMvcResultMatchers.status().isOk()).andReturn();
代码:
@RunWith(SpringRunner.class)
@SpringBootTest
@AutoConfigureMockMvc
public class ControllerTest2 {
@Autowired
private MockMvc mockMvc;
@Test
public void testVideoListApi()throws Exception{
MvcResult mvcResult= mockMvc.perform(MockMvcRequestBuilders.get("/api/v1/pub/video/list"))
.andExpect(MockMvcResultMatchers.status().isOk()).andReturn();
int status=mvcResult.getResponse().getStatus();
System.out.println(status);
String result=mvcResult.getResponse().getContentAsString(Charset.forName("utf-8"));
System.out.println(result);
}