需求
需要在测试类里实现,模拟接口访问。用到MockMvc
实现
测试类里添加一下代码
private MockMvc mvc;
@Before
public void setUp() throws Exception {
mvc = MockMvcBuilders.standaloneSetup(controller).build();
}
@Test
public void getGitHubEntityByUsername() throws Exception {
MvcResult result = mvc.perform(
MockMvcRequestBuilders.get("/github/get/users/Datartisan")
.accept(MediaType.APPLICATION_JSON))
.andReturn();
int statusCode = result.getResponse().getStatus();
Assert.assertEquals(statusCode, 200);
String body = result.getResponse().getContentAsString();
System.out.println("body:"+body);
}