提升测试技能:Junit、Knife4j和JMeter的使用指南

一般常用的测试方法有单元测试、接口测试和并发测试。本文对这三种测试方法在 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.2Knife4j 2.0.0 ~ 2.0.6>=Knife4j 4.0.0
2.2.x~2.4.0Knife4j 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

  1. 引入依赖
    <dependency>
        <groupId>com.github.xiaoymin</groupId>
        <artifactId>knife4j-openapi2-spring-boot-starter</artifactId>
        <version>4.1.0</version>
    </dependency>
    
  2. 配置
    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的属性
序列属性作用
1paramType查询参数类型
path以地址形式提交数据
query直接跟参数完成映射赋值
body以流的形式提交 仅支持POST
header参数在request headers里面提交
form以form表单的形式提交,仅支持POST
2dataType参数的数据类型 只为标志说明 并没有实际验证
Long
String
3name接收参数名
4value接收参数的意义描述
5required参数是否必填
true必填
false非比填
6defaultValue默认值
示例代码
@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™

  1. 下载完成后,双击 bin->ApacheJMeter.jar 运行程序。
    在这里插入图片描述

  2. 右键测试程序,选择添加->线程用户->线程组

    在这里插入图片描述

  3. 配置线程数时间以及循环次数等参数。

    线程数:配置几个就代表有几个虚拟用户

    Ramp-Up 时间(秒):表示从第一个虚拟用户开始生成直到最后一个虚拟用户开始生成的时间,假如配置了5个虚拟用户,Ramp-Up 设置成10s,那么 JMeter 会每隔2s生成1个虚拟用户,Ramp-Up设置成0,那么所有用户将会同时立即产生

    循环数:要么是N次,要么是永远

    调度器:就是定时执行的设置方式。我们可以设置在未来的某个时间开始执行也可以设置多少秒后开始执行。

    注意:你只有在按下执行按钮后,调度器才生效

    在这里插入图片描述

  4. 添加 HTTP 请求配置

    右键线程组->选择添加->取样器->HTTP请求

    在这里插入图片描述

  5. 配置 HTTP 请求

    在这里插入图片描述

  6. 添加查看结果树

    右键线程组->选择添加->监听器->查看结果树

    在这里插入图片描述

  7. 启动测试

    点击启动按钮启动测试。

    在这里插入图片描述

    根据实际情况选择是否保存测试计划。

    在这里插入图片描述

  8. 查看测试结果

    点击查看结果树->选择HTTP请求,查看请求结果。

    在这里插入图片描述


感谢大家读到这里,后续还会有其他相关文章,欢迎继续阅读。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值