SpringBoot测试用例的一些小技巧~

场景一:不想因为测试而对数据库产生脏数据

@Test
public void testInsert() {
	User user = new User();
	user.setUsername("startqbb");
	user.setPassword("123456");
	user.setNickName("秋秋");
	user.setEmail("startqbb@163.com");
	user.setCreateTime(new Date());
	user.setUpdateTime(new Date());
	userService.save(user);
}

我们只需要在测试类似加入事务注解,即可在操作完数据后进行回滚
image

场景二:测试时我们想弄一些随机数据

使用SpringBoot替我们生成的

test:
  user:
    id: ${random.int}
    username: ${random.value}
    password: ${random.uuid}
    nickName: ${random.value}
    email: ${random.value}
    createTime: ${random.long}
    updateTime: ${random.long}

写一个实体类去绑定一些配置信息
image

在需要使用的地方直接注入就好了
image

结果
image

image

posted @   我也有梦想呀  阅读(20)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· winform 绘制太阳,地球,月球 运作规律
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)
点击右上角即可分享
微信分享提示