MockMvc简单使用
1、概念MockMvc是服务端 Spring MVC测试支持的主入口点。可以用来模拟客户端请求,用于测试。2、API(1)@RunWith注解指定测试运行器,例如使用 SpringJUnit4ClassRunner.class(2)@ContextConfiguration注解执行要加载的配置文件,例如 classpath:application.xml 或 file:src/main...
1、概念
MockMvc是服务端 Spring MVC测试支持的主入口点。可以用来模拟客户端请求,用于测试。
2、API
(1)@RunWith注解
指定测试运行器,例如使用 SpringJUnit4ClassRunner.class
(2)@ContextConfiguration注解
执行要加载的配置文件,例如 classpath:application.xml 或 file:src/main/resources/DispatcherServlet-servlet.xml
(3)@WebAppConfiguration注解
用于声明测试时所加载的是WebApplicationContext【WebMVC的 XmlWebApplicationContext 是其实现类】
因为测试需要使用WebMVC对应的IOC容器对象
⑷ WebApplicationContext
WebMVC的IOC容器对象,需要声明并通过@Autowired自动装配进来
⑸ MockMvcRequestBuilders
用于构建MockHttpServletRequestBuilder
- ① get GET请求
- ② post POST请求
- ③ put PUT请求
- ④ delete DELETE请求
- ⑤ param(String name, String… values) 传递参数 K-V…
⑹ MockHttpServletRequestBuilder
用于构建 MockHttpServletRequest,它用于作为 MockMvc的请求对象
⑺ MockMvc
通过 MockMvcBuilders 的 webAppContextSetup(WebApplicationContext context) 方法 获取 DefaultMockMvcBuilder,
再调用 build() 方法,初始化 MockMvc
① perform
perform(RequestBuilder requestBuilder) throws Exception
执行请求,需要传入 MockHttpServletRequest 对象【请求对象】
② andDo
andDo(ResultHandler handler) throws Exception
执行普通处理,例如 MockMvcResultHandlers的print() 方法用于 打印请求、响应及其他相关信息
③ andExpect
andExpect(ResultMatcher matcher) throws Exception
执行预期匹配,例如:
MockMvcResultMatchers.status().isOk() 预期响应成功
MockMvcResultMatchers.content().string(final String expectedContent) 指定预期的返回结果内容[字符串]
MockMvcResultMatchers.content().contentType(String contentType) 指定预期的返回结果的媒体类型
MockMvcResultMatchers.forwardedUrl(final String expectedUrl) 指定预期的请求的URL链接
MockMvcResultMathcers.redirectedUrl(final String expectedUrl) 指定预期的重定向的URL链接
注意:当有一项不满足时,则后续就不会进行。
④ andReturn
andReturn()
返回 MvcResult [请求访问结果]对象
⑤ getRequest
getRequest()
返回 MockHttpServletRequest [请求]对象
常用测试
MockMvcRequestBuilders.
1.测试普通测试器
mockMvc.perform(get("/user/{id}", 1)) //执行请求
.andExpect(model().attributeExists("user")) //验证存储模型数据
.andExpect(view().name("user/view")) //验证viewName
.andExpect(forwardedUrl("/WEB-INF/jsp/user/view.jsp"))//验证视图渲染时forward到的jsp
.andExpect(status().isOk())//验证状态码
.andDo(print()); //输出MvcResult到控制台
2.得到MvcResult自定义验证
MvcResult result = mockMvc.perform(get("/user/{id}", 1))//执行请求
.andReturn(); //返回MvcResult
Assert.assertNotNull(result.getModelAndView().getModel().get("user")); //自定义断言
3.验证请求参数绑定到模型数据及Flash属性
mockMvc.perform(post("/user").param("name", "zhang")) //执行传递参数的POST请求(也可以post("/user?name=zhang"))
.andExpect(handler().handlerType(UserController.class)) //验证执行的控制器类型
.andExpect(handler().methodName("create")) //验证执行的控制器方法名
.andExpect(model().hasNoErrors()) //验证页面没有错误
.andExpect(flash().attributeExists("success")) //验证存在flash属性
.andExpect(view().name("redirect:/user")); //验证视图
4.文件上传
byte[] bytes = new byte[] {1, 2};
mockMvc.perform(fileUpload("/user/{id}/icon", 1L).file("icon", bytes)) //执行文件上传
.andExpect(model().attribute("icon", bytes)) //验证属性相等性
.andExpect(view().name("success")); //验证视图
5.JSON请求/响应验证
String requestBody = "{\"id\":1, \"name\":\"zhang\"}";
mockMvc.perform(post("/user")
.contentType(MediaType.APPLICATION_JSON).content(requestBody)
.accept(MediaType.APPLICATION_JSON)) //执行请求
.andExpect(content().contentType(MediaType.APPLICATION_JSON)) //验证响应contentType
.andExpect(jsonPath("$.id").value(1)); //使用Json path验证JSON 请参考http://goessner.net/articles/JsonPath/
String errorBody = "{id:1, name:zhang}";
MvcResult result = mockMvc.perform(post("/user")
.contentType(MediaType.APPLICATION_JSON).content(errorBody)
.accept(MediaType.APPLICATION_JSON)) //执行请求
.andExpect(status().isBadRequest()) //400错误请求
.andReturn();
Assert.assertTrue(HttpMessageNotReadableException.class.isAssignableFrom(result.getResolvedException().getClass()));//错误的请求内容体
6.异步测试
//Callable
MvcResult result = mockMvc.perform(get("/user/async1?id=1&name=zhang")) //执行请求
.andExpect(request().asyncStarted())
.andExpect(request().asyncResult(CoreMatchers.instanceOf(User.class))) //默认会等10秒超时
.andReturn();
mockMvc.perform(asyncDispatch(result))
.andExpect(status().isOk())
.andExpect(content().contentType(MediaType.APPLICATION_JSON))
.andExpect(jsonPath("$.id").value(1));
7.全局配置
mockMvc = webAppContextSetup(wac)
.defaultRequest(get("/user/1").requestAttr("default", true)) //默认请求 如果其是Mergeable类型的,会自动合并的哦mockMvc.perform中的RequestBuilder
.alwaysDo(print()) //默认每次执行请求后都做的动作
.alwaysExpect(request().attribute("default", true)) //默认每次执行后进行验证的断言
.build();
mockMvc.perform(get("/user/1"))
.andExpect(model().attributeExists("user"));
更多推荐
所有评论(0)