SpringBoot中使用MockMvc测试

需求

需要在测试类里实现,模拟接口访问。用到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);  
    }  

更多参数资料

转载于:https://www.cnblogs.com/kevinyau/p/11084437.html

你可能感兴趣的:(java,测试)