Spring Boot 单元测试详解+实战教程
发布日期:2021-06-30 13:05:22
浏览次数:2
分类:技术文章
本文共 2630 字,大约阅读时间需要 8 分钟。
Spring Boot 的测试类库
Spring Boot 提供了许多实用工具和注解来帮助测试应用程序,主要包括以下两个模块。
spring-boot-test:支持测试的核心内容。
spring-boot-test-autoconfigure:支持测试的自动化配置。
开发进行只要使用 spring-boot-starter-test
启动器就能引入这些 Spring Boot 测试模块,还能引入一些像 JUnit, AssertJ, Hamcrest
及其他一些有用的类库,具体如下所示。
- JUnit:Java 应用程序单元测试标准类库。
- Spring Test & Spring Boot Test:Spring Boot 应用程序功能集成化测试支持。
- AssertJ:一个轻量级的断言类库。
- Hamcrest:一个对象匹配器类库。
- Mockito:一个Java Mock测试框架,默认支付 1.x,可以修改为 2.x。
- JSONassert:一个用于JSON的断言库。
- JsonPath:一个JSON操作类库。
下面是 Maven 的依赖关系图。
以上这些都是 Spring Boot 提供的一些比较常用的测试类库,如果上面的还不能满足你的需要,你也可以随意添加其他的以上没有的类库。
测试 Spring Boot 应用程序
添加 Maven 依赖
org.springframework.boot spring-boot-starter-test 1.5.10.RELEASE test
1、 要让一个普通类变成一个单元测试类只需要在类名上加入 @SpringBootTest 和 @RunWith(SpringRunner.class) 两个注释即可。
2、 在测试方法上加上 @Test 注释。
如果测试需要做 REST 调用,可以 @Autowire 一个 TestRestTemplate。
@RunWith(SpringRunner.class)@SpringBootTestpublic class BBTestAA { @Autowired private TestRestTemplate testRestTemplate; @Test public void testDemo() { ... }}
GET请求测试
@Testpublic void get() throws Exception { MapmultiValueMap = new HashMap<>(); multiValueMap.put("username","Java技术栈"); ActResult result = testRestTemplate.getForObject("/test/getUser?username={username}",ActResult.class,multiValueMap); Assert.assertEquals(result.getCode(),0);}
POST请求测试
@Testpublic void post() throws Exception { MultiValueMap multiValueMap = new LinkedMultiValueMap(); multiValueMap.add("username","Java技术栈"); ActResult result = testRestTemplate.postForObject("/test/post",multiValueMap,ActResult.class); Assert.assertEquals(result.getCode(),0);}
文件上传测试
@Testpublic void upload() throws Exception { Resource resource = new FileSystemResource("/home/javastack/test.jar"); MultiValueMap multiValueMap = new LinkedMultiValueMap(); multiValueMap.add("username","Java技术栈"); multiValueMap.add("files",resource); ActResult result = testRestTemplate.postForObject("/test/upload",multiValueMap,ActResult.class); Assert.assertEquals(result.getCode(),0);}
文件下载测试
@Testpublic void download() throws Exception { HttpHeaders headers = new HttpHeaders(); headers.set("token","javastack"); HttpEntity formEntity = new HttpEntity(headers); String[] urlVariables = new String[]{ "admin"}; ResponseEntityresponse = testRestTemplate.exchange("/test/download?username={1}",HttpMethod.GET,formEntity,byte[].class,urlVariables); if (response.getStatusCode() == HttpStatus.OK) { Files.write(response.getBody(),new File("/home/javastack/test.jar")); }}
推荐:
扫描关注我们的微信公众号,干货每天更新。
转载地址:https://javastack.blog.csdn.net/article/details/80608790 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!
发表评论
最新留言
路过按个爪印,很不错,赞一个!
[***.219.124.196]2024年04月24日 22时32分57秒
关于作者
喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
工厂模式解耦的升级版
2019-05-01
hrm中的jwt认证:获取用户数据
2019-05-01
前端权限控制:获取用户信息接口构造数据
2019-05-01
前端权限控制:前端菜单控制
2019-05-01
前端权限控制:实现思路分析-待修改
2019-05-01
有状态服务和无状态服务
2019-05-01
基于jwt的用户鉴权:配置拦截器并测试
2019-05-01
POI的概述
2019-05-01
POI文件导入:跨服务器调用查询部门信息
2019-05-01
DataURL:概述
2019-05-01
DataURL:实现原理及优缺点分析
2019-05-01
DataURL:实现员工头像保存
2019-05-01
DataURL:员工头像回显
2019-05-01
七牛云存储:通过SDK上传图片
2019-05-01
七牛云存储:断点续传
2019-05-01
七牛云存储:实现员工头像保存
2019-05-01
JasperReport:概述
2019-05-01
JasperReport:声明周期
2019-05-01
递归【应用】
2019-05-01
递归求阶乘
2019-05-01