SpringBoot学习(一)

北慕城南 提交于 2019-11-27 16:16:18

  SpringBoot测试

  

@RunWith(SpringRunner.class)
@SpringBootTest
public class UserControllerTest {
         //用于伪造webMVC环境,模拟tomcat启动后的web程序
    @Autowired
    private WebApplicationContext wac;
    
    private MockMvc mockMvc;
    
    @Before
    public void setup(){
        mockMvc = MockMvcBuilders.webAppContextSetup(wac).build();
    }
    

           //perform表示请求
           //MockMvcRequestBuilders.get("/users")模拟get请求
          //MockMvcRequestBuilders.get("/users") 请求格式
         //andExpect表示服务请求的期望结果
        // MockMvcResultMatchers.status().isOk(),http返回的状态码
       //MockMvcResultMatchers.jsonPath("$.length()").value(3)
      //返回的集合数量(期待返回是一个集合 集合长度是3)
    @Test
    public void whenQuerySuccess() throws Exception{
        mockMvc.perform(MockMvcRequestBuilders.get("/users")
                .contentType(MediaType.APPLICATION_JSON_UTF8)
                ).andExpect(MockMvcResultMatchers.status().isOk())
                .andExpect(MockMvcResultMatchers.jsonPath("$.length()").value(3));
    }
}         

 关于jsonPath说明:https://github.com/json-path/JsonPath

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!