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);  
    }  

更多参数资料

posted @ 2017-08-14 11:01  keivnyau  阅读(126)  评论(0编辑  收藏  举报