测试用例接口开发实战

测试用例接口开发实战

前言

在上一集,我们也大概完成了对Jmeter的二次开发的Demo版本的了解,我们接下来就要基于这个Demo来将Jmeter压测进行平台化。

那么这一集,我们讲一讲测试用例接口开发实战。

StressCaseController

我们的Controller其实也就那些增删改查的操作加上一个执行测试用例的API。

但是我们一开始先不讲执行测试用例的API,先放一放,先把增删改查的API完成。

find

java 复制代码
@RequestMapping("/find")
    public JsonData findById(@RequestParam("projectId")Long projectId,@RequestParam("id")Long caseId){
        return JsonData.buildSuccess(stressCaseService.findById(projectId,caseId));
    }

del

java 复制代码
@PostMapping("/del")
    public JsonData del(@RequestBody StressCaseDelReq req){
        return JsonData.buildSuccess(stressCaseService.delete(req.getId(),req.getProjectId()));
    }
save
java 复制代码
@PostMapping("/save")
    public JsonData save(@RequestBody StressCaseSaveReq req){
        return JsonData.buildSuccess(stressCaseService.save(req));
    }
update
java 复制代码
@PostMapping("/update")
    public JsonData update(@RequestBody StressCaseUpdateReq req){
        return JsonData.buildSuccess(stressCaseService.update(req));
    }
完整代码
java 复制代码
@RestController
@RequestMapping("/api/v1/stress_case")
public class StressCaseController {

    @Resource
    private StressCaseService stressCaseService;

    @RequestMapping("/find")
    public JsonData findById(@RequestParam("projectId")Long projectId,@RequestParam("id")Long caseId){
        return JsonData.buildSuccess(stressCaseService.findById(projectId,caseId));
    }

    @PostMapping("/del")
    public JsonData del(@RequestBody StressCaseDelReq req){
        return JsonData.buildSuccess(stressCaseService.delete(req.getId(),req.getProjectId()));
    }

    @PostMapping("/save")
    public JsonData save(@RequestBody StressCaseSaveReq req){
        return JsonData.buildSuccess(stressCaseService.save(req));
    }
    @PostMapping("/update")
    public JsonData update(@RequestBody StressCaseUpdateReq req){
        return JsonData.buildSuccess(stressCaseService.update(req));
    }

    @GetMapping("/execute")
    public JsonData execute(@RequestParam("projectId")Long projectId,@RequestParam("id")Long caseId){
        return JsonData.buildSuccess(stressCaseService.execute(projectId,caseId));
    }
}

不用多说,自定义请求体类如下

StressCaseSaveReq
java 复制代码
@Data
public class StressCaseSaveReq {

    private Long projectId;

    private Long moduleId;

    private Long environmentId;

    private String name;

    private String description;

    private String assertion;

    private String relation;

    private String stressSourceType;

    private String threadGroupConfig;

    private String jmxUrl;

    private String path;

    private String method;

    private String query;

    private String header;

    private String body;

    private String bodyType;

}
StressCaseDelReq
java 复制代码
@Data
public class StressCaseDelReq {
    private Long id;
    private Long projectId;

}
StressCaseUpdateReq
java 复制代码
@Data
public class StressCaseUpdateReq {
    private Long id;

    private Long projectId;

    private Long moduleId;

    private Long environmentId;

    private String name;

    private String description;

    private String assertion;

    private String relation;

    private String stressSourceType;

    private String threadGroupConfig;

    private String jmxUrl;

    private String path;

    private String method;

    private String query;

    private String header;

    private String body;

    private String bodyType;

}

可以看到,这就是测试用例的自定义请求体类了。

接下来就要完成Service和它的Impl即可。

StressCaseService

java 复制代码
public interface StressCaseService {
    StressCaseDTO findById(Long projectId, Long caseId);

    int delete(Long id, Long projectId);

    int save(StressCaseSaveReq req);

    int update(StressCaseUpdateReq req);

}

StressCaseServiceImpl

findById
java 复制代码
public StressCaseDTO findById(Long projectId, Long caseId) {
        LambdaQueryWrapper<StressCaseDO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(StressCaseDO::getProjectId, projectId);
        queryWrapper.eq(StressCaseDO::getId, caseId);
        StressCaseDO stressCaseDO = stressCaseMapper.selectOne(queryWrapper);
        return SpringBeanUtil.copyProperties(stressCaseDO, StressCaseDTO.class);
    }
delete
java 复制代码
public int delete(Long id, Long projectId) {
        LambdaQueryWrapper<StressCaseDO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(StressCaseDO::getProjectId, projectId);
        queryWrapper.eq(StressCaseDO::getId, id);
        return stressCaseMapper.delete(queryWrapper);
    }
save
java 复制代码
public int save(StressCaseSaveReq req) {
        StressCaseDO stressCaseDO = SpringBeanUtil.copyProperties(req, StressCaseDO.class);
        return stressCaseMapper.insert(stressCaseDO);
    }
update
java 复制代码
 public int update(StressCaseUpdateReq req) {
        StressCaseDO stressCaseDO = SpringBeanUtil.copyProperties(req, StressCaseDO.class);
        LambdaQueryWrapper<StressCaseDO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(StressCaseDO::getProjectId, req.getProjectId());
        queryWrapper.eq(StressCaseDO::getId, req.getId());
        return stressCaseMapper.update(stressCaseDO, queryWrapper);
    }
完整代码
java 复制代码
@Service
public class StressCaseServiceImpl implements StressCaseService {
    @Resource
    private StressCaseMapper stressCaseMapper;

    @Override
    public StressCaseDTO findById(Long projectId, Long caseId) {
        LambdaQueryWrapper<StressCaseDO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(StressCaseDO::getProjectId, projectId);
        queryWrapper.eq(StressCaseDO::getId, caseId);
        StressCaseDO stressCaseDO = stressCaseMapper.selectOne(queryWrapper);
        return SpringBeanUtil.copyProperties(stressCaseDO, StressCaseDTO.class);
    }

    @Override
    public int delete(Long id, Long projectId) {
        LambdaQueryWrapper<StressCaseDO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(StressCaseDO::getProjectId, projectId);
        queryWrapper.eq(StressCaseDO::getId, id);
        return stressCaseMapper.delete(queryWrapper);
    }

    @Override
    public int save(StressCaseSaveReq req) {
        StressCaseDO stressCaseDO = SpringBeanUtil.copyProperties(req, StressCaseDO.class);
        return stressCaseMapper.insert(stressCaseDO);
    }

    @Override
    public int update(StressCaseUpdateReq req) {
        StressCaseDO stressCaseDO = SpringBeanUtil.copyProperties(req, StressCaseDO.class);
        LambdaQueryWrapper<StressCaseDO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(StressCaseDO::getProjectId, req.getProjectId());
        queryWrapper.eq(StressCaseDO::getId, req.getId());
        return stressCaseMapper.update(stressCaseDO, queryWrapper);
    }
}
StressCaseDTO
java 复制代码
@Data
public class StressCaseDTO implements Serializable {


    private Long id;

    private Long projectId;

    private Long moduleId;

    private Long environmentId;

    private String name;

    private String description;

    private String assertion;

    private String relation;

    private String stressSourceType;

    private String threadGroupConfig;

    private String jmxUrl;

    private String path;

    private String method;

    private String query;

    private String header;

    private String body;

    private String bodyType;

    private Date gmtCreate;

    private Date gmtModified;
}
相关推荐
飞的肖4 分钟前
前端使用 Element Plus架构vue3.0实现图片拖拉拽,后等比压缩,上传到Spring Boot后端
前端·spring boot·架构
Q_19284999067 分钟前
基于Spring Boot的摄影器材租赁回收系统
java·spring boot·后端
Code_流苏9 分钟前
VSCode搭建Java开发环境 2024保姆级安装教程(Java环境搭建+VSCode安装+运行测试+背景图设置)
java·ide·vscode·搭建·java开发环境
良许Linux11 分钟前
0.96寸OLED显示屏详解
linux·服务器·后端·互联网
求知若饥23 分钟前
NestJS 项目实战-权限管理系统开发(六)
后端·node.js·nestjs
禁默1 小时前
深入浅出:AWT的基本组件及其应用
java·开发语言·界面编程
Cachel wood1 小时前
python round四舍五入和decimal库精确四舍五入
java·linux·前端·数据库·vue.js·python·前端框架
Code哈哈笑1 小时前
【Java 学习】深度剖析Java多态:从向上转型到向下转型,解锁动态绑定的奥秘,让代码更优雅灵活
java·开发语言·学习
gb42152871 小时前
springboot中Jackson库和jsonpath库的区别和联系。
java·spring boot·后端
程序猿进阶1 小时前
深入解析 Spring WebFlux:原理与应用
java·开发语言·后端·spring·面试·架构·springboot