springboot controller测试用例

@RunWith(SpringRunner.class)

@SpringBootTest

public class ControllerClassTest {

    private MockMvc mockMvc;

    @Autowired

    private WebApplicationContext wac;


    @Before public void before(){

            this.mockMvc = MockMvcBuilders.webAppContextSetup(this.wac).build();

         }


    @Test

    public void testDetail() throws Exception {

            String jsonStr = "{aa:bb}";

            MvcResult result = mockMvc.perform(get("/XX/YY").param("json",jsonStr))             .andExpect(status().isOk())             .andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8)) .andReturn();             JSONObject jsonResult =                                 JSONObject.parseObject(result.getResponse().getContentAsString());                           Assert.assertNotNull(jsonResult.getString("XX"));

    }

}

你可能感兴趣的:(springboot controller测试用例)