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 @ 2023-02-05 00:28  我也有梦想呀  阅读(23)  评论(0)    收藏  举报