Spring Boot Junit单元测试MockMvc使用(MockHttpServletRequestBuilder MvcResult ModelAndView json)

				版权声明:复制就复制吧,有用就点赞或者评论一下,好歹我知道能帮上你了					https://blog.csdn.net/qq_16513911/article/details/83018027				</div>
							<link rel="stylesheet" href="https://csdnimg.cn/release/phoenix/template/css/ck_htmledit_views-f57960eb32.css">
							            <link rel="stylesheet" href="https://csdnimg.cn/release/phoenix/template/css/ck_htmledit_views-f57960eb32.css">
					<div class="htmledit_views" id="content_views">
            <p>项目中使用了<span style="color:#f33b45;">MockMvcRequestBuilders</span>这个类来实现单元测试</p>

特此举例get和post请求带Cookie,带参数,带对象,表单提交,url路径提交,

针对@ModelAttribute,@RequestBody传参传对象

获取返回对象MockHttpServletRequestBuilder  MvcResult  ModelAndView,返回视图/json/字符串解析

故作笔记,也方便日后查阅复习.

单元测试框必要的注入都要有,一个小的框架就出来了


 
 
  1. @RunWith(SpringRunner.class)
  2. @SpringBootTest
  3. public class domeTest {
  4. @Autowired
  5. private WebApplicationContext context;
  6. protected MockMvc mvc;
  7. @Before
  8. public void setUp() {
  9. mvc = MockMvcBuilders.webAppContextSetup(context).build();
  10. }
  11. @Test
  12. public void doem() {
  13. }
  14. }

1.各种请求入参


 
 
  1. //发送请求举例
  2. //post请求
  3. MockHttpServletRequestBuilder post = MockMvcRequestBuilders.post( "/test");
  4. //get请求
  5. MockHttpServletRequestBuilder get = MockMvcRequestBuilders.get( "/test");
  6. //post请求 带Cookie
  7. MockHttpServletRequestBuilder post_cookie = MockMvcRequestBuilders.post( "/test")
  8. .cookie( new Cookie( "cookieName", "value")); //自己填键值对
  9. //get请求 带Cookie
  10. MockHttpServletRequestBuilder get_cookie = MockMvcRequestBuilders.post( "/test")
  11. .cookie( new Cookie( "cookieName", "value")); //自己填键值对
  12. //post请求 带session
  13. MockHttpServletRequestBuilder post_cookie_session = MockMvcRequestBuilders.post( "/test")
  14. .sessionAttr( "sessionName", "value");
  15. //get请求 带session
  16. MockHttpServletRequestBuilder get_cookie_session = MockMvcRequestBuilders.get( "/test")
  17. .sessionAttr( "sessionName", "value");
  18. //post请求 带Cookie 带参
  19. //另外也可适用场景 使用@ModelAttribute("formName")注解接收form表单对象
  20. //例子:
  21. // @PostMapping("/submitOrder")
  22. // public ModelAndView submitOrder (@ModelAttribute("orderForm") ServiceProductOrder serviceProductOrder)
  23. MockHttpServletRequestBuilder post_cookie_par = MockMvcRequestBuilders.post( "/test")
  24. .cookie( new Cookie( "cookieName", "value")) //自己填键值对
  25. .param( "userName", "admin") //有@RequestParam注解优先对应,否则对应着表单的input标签的name属性的 值及value
  26. .param( "pass", "admin"); //用@ModelAttribute注解的直接对应表单内容
  27. //get请求 带Cookie 带参方法一(用方法填充)
  28. MockHttpServletRequestBuilder get_cookie_par_one = MockMvcRequestBuilders.post( "/test")
  29. .cookie( new Cookie( "cookieName", "value")) //自己填键值对
  30. //有@RequestParam注解优先对应,否则对应着表单的input标签的name属性的 值及value
  31. .param( "userName", "admin")
  32. .param( "pass", "admin");
  33. //get请求 带Cookie 带参方法二(url路径拼接)
  34. MockHttpServletRequestBuilder get_cookie_par_two = MockMvcRequestBuilders.post( "/test?userName=admin&pass=admin")
  35. .param( "pass", "admin");
  36. //post请求 带Cookie
  37. //适用场景:使用@RequestBody注解接收对象
  38. //例子:@PostMapping("/submitOrder")
  39. // public ModelAndView submitOrder (@RequestBody ServiceProductOrder serviceProductOrder) {
  40. Admin admin= new Admin();
  41. admin.setLoginName( "admin");
  42. admin.setLoginPassword( "admin"); //填一些必要的参数等.
  43. MockHttpServletRequestBuilder post_cookie_obj = MockMvcRequestBuilders.post( "/test")
  44. .cookie( new Cookie( "cookieName", "value")) //自己填键值对
  45. .contentType(MediaType.APPLICATION_JSON).content(JSONObject.toJSONString(admin)); //阿里巴巴的json序列化
  46. //MultipartFile文件上传请求
  47. String filename = "images/sy_02.png"; //测试文件
  48. InputStream inStream = getClass().getClassLoader().getResourceAsStream(filename);
  49. MockMultipartFile mfile = new MockMultipartFile( "file", "sy_02.png", "png", inStream);
  50. MockMultipartHttpServletRequestBuilder file = MockMvcRequestBuilders.multipart( "/file/upload").file(mfile);

 2.各种请求Response处理

上面的代码中都返回了MockHttpServletRequestBuilder(以及文件的MockMultipartHttpServletRequestBuilder)对象,接下来就是执行判断返回内容,这些返回值都继承于RequestBuilder

 
 

 


 
 
  1. //执行请求 返回相应的MvcResult (mvc这个参数,看最上面的框架怎么注入的)
  2. MvcResult mvcResult = mvc.perform(request).andReturn();
  3. //获取状态码
  4. int status = mvcResult.getResponse().getStatus(); //500状态码 302状态码 404状态码 200状态码等
  5. //获取返回 @ResponseBody json字符串 : 进行反序列化处理即可
  6. //注意: 500/400/302则是返回的HTML源码String类型
  7. String contentAsString = mvcResult.getResponse().getContentAsString();
  8. //返回ModelAndView 获取里面的页面路径
  9. //代码: model.setViewName("/index");
  10. ModelAndView mv = mvcResult.getModelAndView();
  11. String url=mv.getViewName(); //得到/index
  12. //返回ModelAndView 判断里面state参数
  13. // model.addObject("state", 1);
  14. ModelAndView mv = mvcResult.getModelAndView();
  15. Map<String, Object> model = mv.getModel();
  16. Integer state = (Integer) model.get( "state");
  17. //返回ModelAndView 判断里面的集合/map/对象
  18. // PageInfo<Admin> list=new PageInfo<>();
  19. // model.addObject("AdminList", list);
  20. ModelAndView mv1 = mvcResult.getModelAndView();
  21. Map<String, Object> model1 = mv1.getModel();
  22. ((PageInfo<Admin>) model1.get( "AdminList")).getList().size(); //得到list长度

ps:返回的json字符串我个人喜欢使用转化为JSONObject再加上Assert断言使用,也挺方便的


 
 
  1. JSONObject jsonObject = JSONObject.parseObject(jsonString);
  2. Assert.assertEquals( 0,jsonObject.getIntValue( "code"));

写代码时翻的资料:

Junit测试Controller(MockMVC使用),传输@RequestBody数据解决办法

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值