一般常用的测试方法有单元测试、接口测试和并发测试。本文对这三种测试方法在 JAVA 开发中常用的工具配置及使用做一个简单介绍。
- 单元测试
对对象所属模块的后台测试,对业务层进行测试。 - 接口测试
对软件系统的接口进行测试,即验证不同模块之间的通信是否正常、参数传递是否正确、错误处理是否正确等。 - 并发测试
对系统或网络在高负载和大并发访问情况下的性能和稳定性进行测试。
Junit(Spring Boot项目中常用的单元测试工具)
引入依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
测试用例
@SpringBootTest
@RunWith(SpringJUnit4ClassRunner.class)
public class EatImplTest {
@Autowired
private IEat ie;
@Test
public void eat() {
int sum = ie.eat();
assertEquals(5060,sum);
}
}
Knife4j(Swagger的本地增强解决方案)
Knife4j与springboot的兼容性
Spring Boot版本 | Knife4j Swagger2规范 | Knife4j OpenAPI3规范 |
---|---|---|
1.5.x~2.0.0 | <Knife4j 2.0.0 | >=Knife4j 4.0.0 |
2.0~2.2 | Knife4j 2.0.0 ~ 2.0.6 | >=Knife4j 4.0.0 |
2.2.x~2.4.0 | Knife4j 2.0.6 ~ 2.0.9 | >=Knife4j 4.0.0 |
2.4.0~2.7.x | >=Knife4j 4.0.0 | >=Knife4j 4.0.0 |
>= 3.0 | >=Knife4j 4.0.0 | >=Knife4j 4.0.0 |
SpringBoot 项目引入 Knife4j
- 引入依赖
<dependency> <groupId>com.github.xiaoymin</groupId> <artifactId>knife4j-openapi2-spring-boot-starter</artifactId> <version>4.1.0</version> </dependency>
- 配置
knife4j: enable: true #此项必配置,否则knife4j运行有问题 openapi: title: knife4j项目的名称 description: knife4j项目的描述 email: 项目邮箱 concat: 贡献者名称 url: 项目的地址 license: Apache 2.0 license-url: http://www.apache.org/licenses/LICENSE-2.0 terms-of-service-url: https://www.bilibili.com/video/BV1tK411y7Uf/
Knife4j 常用注解
在返回对象上使用
在HttpResp(用户封装返回对象的类)
序列 | 注解 | 作用 |
---|---|---|
1 | @ApiModel | 用对象来接收参数 |
2 | @ApiModelProperty | 用对象接收参数时,描述对象的一个字段 |
在Controller类和方法上使用
序列 | 注解 | 作用 |
---|---|---|
1 | @Api(tags = “Knife4jController接口类”) | |
2 | @Api | 修饰整个类,描述api(Controller)的作用 |
3 | @ApiOperation | 描述一个类的一个方法,或者说一个接口 |
4 | @ApiImplicitParams | 多个请求参数的描述信息 |
5 | @ApiImplicitParam | (name = “name”,value=”请求的名称”,required = true) |
6 | @ApiIgnore | 使用该注解忽略这个API |
7 | @ApiError | 发生错误返回的信息 |
@ApiImplicitParm的属性
序列 | 属性 | 值 | 作用 |
---|---|---|---|
1 | paramType | 查询参数类型 | |
path | 以地址形式提交数据 | ||
query | 直接跟参数完成映射赋值 | ||
body | 以流的形式提交 仅支持POST | ||
header | 参数在request headers里面提交 | ||
form | 以form表单的形式提交,仅支持POST | ||
2 | dataType | 参数的数据类型 只为标志说明 并没有实际验证 | |
Long | |||
String | |||
3 | name | 接收参数名 | |
4 | value | 接收参数的意义描述 | |
5 | required | 参数是否必填 | |
true | 必填 | ||
false | 非比填 | ||
6 | defaultValue | 默认值 |
示例代码
@Api(tags = "图书类型接口")
@RestController
@RequestMapping("/type")
@Slf4j
public class BookController {
private int total =0;
@ApiOperation(value = "findAll", notes = "图书类型查询")
@GetMapping("/findAll")
// public HttpResp<List<Type>> findAll() {
public HttpResp<Integer> findAll() {
// List<Type> types = new LinkedList();
// for (int i = 0; i < 100; i++) {
// types.add(new Type(i, "type_" + i, "admin", new Date(), new Date()));
// }
total += 1;
log.debug("total-->{}", total);
return new HttpResp<>(200, "成功", total, new Date());
}
@ApiOperation(value = "addType", notes = "添加新的图书接口类型")
@ApiImplicitParams({
@ApiImplicitParam(name = "name", value = "请输入新的图书类型名称", required = true)
})
@PostMapping("/addType")
public HttpResp<String> addType(String name) {
log.debug("添加新的类型:{}", name);
return new HttpResp<>(200, "success", name, new Date());
}
@ApiOperation(value = "excel", notes = "上传excel文件进行解析")
// @ApiImplicitParam(value = "上传文件", required = true)
@PostMapping("/uploadExcel")
public HttpResp<String> uploadExcel(@RequestPart @RequestParam("excel") MultipartFile excel){
String originalFilename = excel.getOriginalFilename();
log.debug("上传文件名称:{}", originalFilename);
return new HttpResp<>(200,"success",originalFilename+"上传成功",new Date());
}
}
效果如下图:
JMeter
官网:Apache JMeter - Apache JMeter™
-
下载完成后,双击
bin->ApacheJMeter.jar
运行程序。
-
右键测试程序,选择添加->线程用户->线程组
-
配置线程数、时间以及循环次数等参数。
线程数:配置几个就代表有几个虚拟用户
Ramp-Up 时间(秒):表示从第一个虚拟用户开始生成直到最后一个虚拟用户开始生成的时间,假如配置了5个虚拟用户,Ramp-Up 设置成10s,那么 JMeter 会每隔2s生成1个虚拟用户,Ramp-Up设置成0,那么所有用户将会同时立即产生
循环数:要么是N次,要么是永远
调度器:就是定时执行的设置方式。我们可以设置在未来的某个时间开始执行也可以设置多少秒后开始执行。
注意:你只有在按下执行按钮后,调度器才生效
-
添加 HTTP 请求配置
右键线程组->选择添加->取样器->HTTP请求
-
配置 HTTP 请求
-
添加查看结果树
右键线程组->选择添加->监听器->查看结果树
-
启动测试
点击启动按钮启动测试。
根据实际情况选择是否保存测试计划。
-
查看测试结果
点击查看结果树->选择HTTP请求,查看请求结果。
感谢大家读到这里,后续还会有其他相关文章,欢迎继续阅读。