0


《SpringBoot篇》14.@AutoConfigureMockMvc测试类实现Web层测试

陈老老老板

说明:学习一些新的技术栈,边学习边总结,各位一起加油。需要注意的地方都标红了,还有资源的分享. 一起加油。

说明:其实使用PostMan或者PostApi等软件测试效果是一样的,但是又不能不知道可以在测试类中实现,就当拓展知识了解吧。之后我会写一篇关于各种测试软件不同点的博客,可以关注一下。

1.测试类实现Web层测试

说明:本文是在springboot项目中实现的哦。

(1)在测试类上添加@AutoConfigureMockMvc

@SpringBootTest()@AutoConfigureMockMvcclassDemoApplicationTests{@TestvoidWebFindTest(){}}

(2)在SpringBootTest注解中添加参数webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT

@SpringBootTest(webEnvironment =SpringBootTest.WebEnvironment.RANDOM_PORT)//开启虚拟MVC调用@AutoConfigureMockMvcpublicclassWebTest{@TestvoidtestWeb(@AutowiredMockMvc mvc){}}

注:这里WebEnvironment几个参数的含义:

  • MOCK:根据当前设置确认是否启动web环境,例如使用了Servlet的API就启动web环境,属于适配性的配置
  • DEFINED_PORT:使用自定义的端口作为web服务器端口
  • RANDOM_PORT:使用随机端口作为web服务器端口(建议使用,防止端口冲突)
  • NONE:不启动web环境

(3)引入MockMvc对象创建访问请求

注:测试可以发现请求成功,测试发送请求可以成功但是无法看到结果,在下一步就可以看到了。
注:web层Controller代码

packagecom.example.demo.controller;importcom.example.demo.pojo.Admin;importcom.example.demo.repository.AdminRepository;importorg.springframework.beans.factory.annotation.Autowired;importorg.springframework.web.bind.annotation.GetMapping;importorg.springframework.web.bind.annotation.ResponseBody;importorg.springframework.web.bind.annotation.RestController;importjava.util.List;@RestControllerpublicclassWebController{@GetMapping("findWeb")@ResponseBodypublicStringfinall(){return"Web测试成功";}}

注:测试类代码

packagecom.example.demo;importorg.junit.jupiter.api.Test;importorg.springframework.beans.factory.annotation.Autowired;importorg.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;importorg.springframework.boot.test.context.SpringBootTest;importorg.springframework.test.web.servlet.MockMvc;importorg.springframework.test.web.servlet.request.MockHttpServletRequestBuilder;importorg.springframework.test.web.servlet.request.MockMvcRequestBuilders;@SpringBootTest(webEnvironment =SpringBootTest.WebEnvironment.RANDOM_PORT)@AutoConfigureMockMvcclassDemoApplicationTests{//引入MockMvc类型对象@AutowiredprivateMockMvc mvc;@TestvoidWebFindTest()throwsException{//http://localhost:8080/findWeb//创建虚拟请求//前面的服务器IP地址和端口使用的是当前虚拟的web环境,无需指定,仅指定请求的具体路径即可MockHttpServletRequestBuilder builder =MockMvcRequestBuilders.get("/findWeb");//这里需要抛出异常
        mvc.perform(builder);}}

测试成功效果:
在这里插入图片描述

(4)实现请求结果对比

注: 响应对比有四种类型,响应状态匹配、响应体匹配(非json数据格式)、响应体匹配(json数据格式,开发中的主流使用方式)、响应头信息匹配。

a.响应状态匹配

packagecom.example.demo;importorg.junit.jupiter.api.Test;importorg.springframework.beans.factory.annotation.Autowired;importorg.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;importorg.springframework.boot.test.context.SpringBootTest;importorg.springframework.test.web.servlet.MockMvc;importorg.springframework.test.web.servlet.ResultActions;importorg.springframework.test.web.servlet.ResultMatcher;importorg.springframework.test.web.servlet.request.MockHttpServletRequestBuilder;importorg.springframework.test.web.servlet.request.MockMvcRequestBuilders;importorg.springframework.test.web.servlet.result.MockMvcResultMatchers;importorg.springframework.test.web.servlet.result.StatusResultMatchers;@SpringBootTest(webEnvironment =SpringBootTest.WebEnvironment.RANDOM_PORT)@AutoConfigureMockMvcclassDemoApplicationTests{//引入MockMvc类型对象@AutowiredprivateMockMvc mvc;@TestvoidWebFindTest()throwsException{//http://localhost:8080/findWeb//创建虚拟请求//前面的服务器IP地址和端口使用的是当前虚拟的web环境,无需指定,仅指定请求的具体路径即可MockHttpServletRequestBuilder builder =MockMvcRequestBuilders.get("/findWeb");//这里需要抛出异常//获取返回值ResultActions perform = mvc.perform(builder);//设定预期值 与真实值进行比较,成功测试通过,失败测试失败//定义本次调用的预期值StatusResultMatchers status =MockMvcResultMatchers.status();//预计本次调用时成功的:状态200ResultMatcher ok = status.isOk();//添加预计值到本次调用过程中进行匹配
        perform.andExpect(ok);}}

b.响应体匹配(非json数据格式)

packagecom.example.demo;importorg.junit.jupiter.api.Test;importorg.springframework.beans.factory.annotation.Autowired;importorg.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;importorg.springframework.boot.test.context.SpringBootTest;importorg.springframework.test.web.servlet.MockMvc;importorg.springframework.test.web.servlet.ResultActions;importorg.springframework.test.web.servlet.ResultMatcher;importorg.springframework.test.web.servlet.request.MockHttpServletRequestBuilder;importorg.springframework.test.web.servlet.request.MockMvcRequestBuilders;importorg.springframework.test.web.servlet.result.ContentResultMatchers;importorg.springframework.test.web.servlet.result.MockMvcResultMatchers;importorg.springframework.test.web.servlet.result.StatusResultMatchers;@SpringBootTest(webEnvironment =SpringBootTest.WebEnvironment.RANDOM_PORT)@AutoConfigureMockMvcclassDemoApplicationTests{//引入MockMvc类型对象@AutowiredprivateMockMvc mvc;@TestvoidWebFindTest()throwsException{//http://localhost:8080/findWeb//创建虚拟请求//前面的服务器IP地址和端口使用的是当前虚拟的web环境,无需指定,仅指定请求的具体路径即可MockHttpServletRequestBuilder builder =MockMvcRequestBuilders.get("/findWeb");//这里需要抛出异常//获取返回值ResultActions perform = mvc.perform(builder);//设定预期值 与真实值进行比较,成功测试通过,失败测试失败//定义本次调用的预期值ContentResultMatchers content =MockMvcResultMatchers.content();//预计本次调用时成功的:状态200ResultMatcher resultMatcher = content.string("Web测试成功");//添加预计值到本次调用过程中进行匹配
        perform.andExpect(resultMatcher);}}

测试成功将会访问成功,给大家看一下失败的样式:(会报错)
在这里插入图片描述

c.响应体匹配(json数据格式,开发中的主流使用方式)

Controller类也要改成返回Json

packagecom.example.demo.controller;importorg.springframework.web.bind.annotation.GetMapping;importorg.springframework.web.bind.annotation.ResponseBody;importorg.springframework.web.bind.annotation.RestController;@RestControllerpublicclassWebController{@GetMapping("findWeb")@ResponseBodypublicStringfinall(){return"{\"name\" : Web测试成功}";}}

注意测试类Json写法:

packagecom.example.demo;importorg.junit.jupiter.api.Test;importorg.springframework.beans.factory.annotation.Autowired;importorg.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;importorg.springframework.boot.test.context.SpringBootTest;importorg.springframework.test.web.servlet.MockMvc;importorg.springframework.test.web.servlet.ResultActions;importorg.springframework.test.web.servlet.ResultMatcher;importorg.springframework.test.web.servlet.request.MockHttpServletRequestBuilder;importorg.springframework.test.web.servlet.request.MockMvcRequestBuilders;importorg.springframework.test.web.servlet.result.ContentResultMatchers;importorg.springframework.test.web.servlet.result.MockMvcResultMatchers;importorg.springframework.test.web.servlet.result.StatusResultMatchers;@SpringBootTest(webEnvironment =SpringBootTest.WebEnvironment.RANDOM_PORT)@AutoConfigureMockMvcclassDemoApplicationTests{//引入MockMvc类型对象@AutowiredprivateMockMvc mvc;@TestvoidWebFindTest()throwsException{//http://localhost:8080/findWeb//创建虚拟请求//前面的服务器IP地址和端口使用的是当前虚拟的web环境,无需指定,仅指定请求的具体路径即可MockHttpServletRequestBuilder builder =MockMvcRequestBuilders.get("/findWeb");//这里需要抛出异常//获取返回值ResultActions perform = mvc.perform(builder);//设定预期值 与真实值进行比较,成功测试通过,失败测试失败//定义本次调用的预期值ContentResultMatchers content =MockMvcResultMatchers.content();//这里注意Json写法格式ResultMatcher result = content.json("{\"name\":Web测试成功}");//添加预计值到本次调用过程中进行匹配
        perform.andExpect(result);}}

d.响应头信息匹配

packagecom.example.demo;importorg.junit.jupiter.api.Test;importorg.springframework.beans.factory.annotation.Autowired;importorg.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;importorg.springframework.boot.test.context.SpringBootTest;importorg.springframework.test.web.servlet.MockMvc;importorg.springframework.test.web.servlet.ResultActions;importorg.springframework.test.web.servlet.ResultMatcher;importorg.springframework.test.web.servlet.request.MockHttpServletRequestBuilder;importorg.springframework.test.web.servlet.request.MockMvcRequestBuilders;importorg.springframework.test.web.servlet.result.ContentResultMatchers;importorg.springframework.test.web.servlet.result.HeaderResultMatchers;importorg.springframework.test.web.servlet.result.MockMvcResultMatchers;importorg.springframework.test.web.servlet.result.StatusResultMatchers;@SpringBootTest(webEnvironment =SpringBootTest.WebEnvironment.RANDOM_PORT)@AutoConfigureMockMvcclassDemoApplicationTests{//引入MockMvc类型对象@AutowiredprivateMockMvc mvc;@TestvoidWebFindTest()throwsException{//http://localhost:8080/findWeb//创建虚拟请求//前面的服务器IP地址和端口使用的是当前虚拟的web环境,无需指定,仅指定请求的具体路径即可MockHttpServletRequestBuilder builder =MockMvcRequestBuilders.get("/findWeb");//这里需要抛出异常//获取返回值ResultActions perform = mvc.perform(builder);//响应头HeaderResultMatchers header =MockMvcResultMatchers.header();ResultMatcher contentType = header.string("Content-Type","text/plain;charset=UTF-8");
        perform.andExpect(contentType);}}

(4)校验总结

注: 平时都是三种校验一起,(Json与非Json只能一种)

packagecom.example.demo;importorg.junit.jupiter.api.Test;importorg.springframework.beans.factory.annotation.Autowired;importorg.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;importorg.springframework.boot.test.context.SpringBootTest;importorg.springframework.test.web.servlet.MockMvc;importorg.springframework.test.web.servlet.ResultActions;importorg.springframework.test.web.servlet.ResultMatcher;importorg.springframework.test.web.servlet.request.MockHttpServletRequestBuilder;importorg.springframework.test.web.servlet.request.MockMvcRequestBuilders;importorg.springframework.test.web.servlet.result.ContentResultMatchers;importorg.springframework.test.web.servlet.result.HeaderResultMatchers;importorg.springframework.test.web.servlet.result.MockMvcResultMatchers;importorg.springframework.test.web.servlet.result.StatusResultMatchers;@SpringBootTest(webEnvironment =SpringBootTest.WebEnvironment.RANDOM_PORT)@AutoConfigureMockMvcclassDemoApplicationTests{//引入MockMvc类型对象@AutowiredprivateMockMvc mvc;@TestvoidWebFindTest()throwsException{//http://localhost:8080/findWeb//创建虚拟请求//前面的服务器IP地址和端口使用的是当前虚拟的web环境,无需指定,仅指定请求的具体路径即可MockHttpServletRequestBuilder builder =MockMvcRequestBuilders.get("/findWeb");//这里需要抛出异常//获取返回值ResultActions perform = mvc.perform(builder);//设定预期值 与真实值进行比较,成功测试通过,失败测试失败//定义本次调用的预期值//响应状态StatusResultMatchers status =MockMvcResultMatchers.status();ResultMatcher ok = status.isOk();
        perform.andExpect(ok);//响应头HeaderResultMatchers header =MockMvcResultMatchers.header();ResultMatcher contentType = header.string("Content-Type","text/plain;charset=UTF-8");
        perform.andExpect(contentType);//返回的响应体ContentResultMatchers content =MockMvcResultMatchers.content();ResultMatcher result = content.json("{\"name\":Web测试成功}");
        perform.andExpect(result);}}

2.补充测试层实现数据回滚

说明:使用@Transactional,@Rollback(true)防止执行测试类产生垃圾数据。

packagecom.example.demo;importcom.example.demo.pojo.Admin;importcom.example.demo.repository.AdminRepository;importorg.junit.jupiter.api.Test;importorg.springframework.beans.factory.annotation.Autowired;importorg.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;importorg.springframework.boot.test.context.SpringBootTest;importorg.springframework.test.annotation.Rollback;importorg.springframework.test.web.servlet.MockMvc;importorg.springframework.test.web.servlet.ResultActions;importorg.springframework.test.web.servlet.ResultMatcher;importorg.springframework.test.web.servlet.request.MockHttpServletRequestBuilder;importorg.springframework.test.web.servlet.request.MockMvcRequestBuilders;importorg.springframework.test.web.servlet.result.HeaderResultMatchers;importorg.springframework.test.web.servlet.result.MockMvcResultMatchers;importorg.springframework.transaction.annotation.Transactional;importjava.util.Date;@SpringBootTest()@Transactional@RollbackclassRollbackTests{@AutowiredprivateAdminRepository adminRepository;@TestvoidWebFindTest(){Admin admin  =newAdmin();
        admin.setPassword("123");
        adminRepository.save(admin);}}

3.补充测试用例数据随机

说明: 在配置文件中设置数据为随机,配合@ConfigurationProperties实现随机值注入

test:
  book:
    id: ${random.int}
    id2: ${random.int(10)}
    type: ${random.int!5,10!}
    name: ${random.value}
    uuid: ${random.uuid}
    publishTime: ${random.long}
@Component@Data@ConfigurationProperties(prefix ="test.book")publicclassBookCase{privateint id;privateint id2;privateint type;privateString name;privateString uuid;privatelong publishTime;}

**总结:这篇是对测试类进行知识补充,多了解一下,总是没有坏处的。希望对您有帮助,感谢阅读

结束语:裸体一旦成为艺术,便是最圣洁的。道德一旦沦为虚伪,便是最下流的。
勇敢去做你认为正确的事,不要被世俗的流言蜚语所困扰。**


本文转载自: https://blog.csdn.net/weixin_47343544/article/details/127165208
版权归原作者 陈老老老板 所有, 如有侵权,请联系我们删除。

“《SpringBoot篇》14.@AutoConfigureMockMvc测试类实现Web层测试”的评论:

还没有评论