数据层测试事务回滚
pom.xml导入对应的一些坐标,mysql,Mp,等
1 | com.baomidoumybatis-plus-boot-starter3.5.2org.projectlomboklomboktruemysqlmysql-connector-javaruntime |
dao下
1 2 3 4 5 6 7 8 9 10 | import com.baomidou.mybatisplus.core.mapper.BaseMapper; import com.pojo.Person; import org.apache.ibatis.annotations.Mapper; import org.mybatis.spring.annotation.MapperScan; import org.springframework.stereotype.Component; import org.springframework.stereotype.Repository; @Mapper //使用注解配置映射 @Component //给spring管理,方便注入 public interface PersonDao extends BaseMapper { } |
pojo对象
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | package com.pojo; import com.baomidou.mybatisplus.annotation.IdType; import com.baomidou.mybatisplus.annotation.TableId; import com.baomidou.mybatisplus.annotation.TableName; import lombok.Data; @Data @TableName ( "tb_user" ) public class Person { private Long id; private String username; private String password; private String gender; private String addr; } |
service
1 2 3 4 5 6 | package com.service; import com.baomidou.mybatisplus.core.metadata.IPage; import com.baomidou.mybatisplus.extension.service.IService; import com.pojo.Person; public interface PersonService extends IService { } |
serviceImpl
1 2 3 | @Service public class PersonServiceImpl extends ServiceImpl implements PersonService { } |
PersonServiceTest类下
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | package com.serviceTest; import com.pojo.Person; import com.service.PersonService; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.annotation.Rollback; import org.springframework.transaction.annotation.Transactional; @SpringBootTest @Transactional @Rollback ( false ) public class PersonServiceTest { @Autowired private PersonService personService; @Test void testAdd(){ Person person = new Person(); person.setUsername( "测试回滚2" ); person.setPassword( "1" ); person.setGender( "1" ); person.setAddr( "1" ); System.out.println(personService.save(person)); } } |
加上@Transactional运行
加上@Transactional和@Rollback(false)运行
为了测试用例添加事务,加上@Transactional,SpringBoot会对测试用例对应的事务提交操作进行回滚,也就是springboot识别到这个是test,所以不会进行提交事务,但是会占用id。不会有数据显示。
如果想在测试用例中提交事务,可以通过@Rollback(false),不回滚,默认值是true,加上false就不会回滚,测试数据就能在数据库中显示出来。
测试用例数据设定
测试用例数据通常采用随机值进行测试,使用SpringBoot提供的随机数位器赋值
${random.int}表示随机整数
${random.int(10)}表示10以内的随机数
${random.int(10,20)}表示10到20的随机数
其中()可以是任意字符,如[ ],@@都可以。
配置文件下
personRandom:
age: ${random.int(1,100)}
name: ${random.value}
detail: ${random.uuid}
定义一个类接收
1 2 3 4 5 6 7 8 | @Data @Component //给spring管理 @ConfigurationProperties (prefix = "personrandom" ) public class Person { private String name; private String age; private String detail; } |
测试类下
1 2 3 4 5 6 7 8 9 | @SpringBootTest public class RandomTest { @Autowired private Person person; @Test public void KC(){ System.out.println(person); } } |
运行结果
到此这篇关于SpringBoot数据层测试事务回滚的实现流程的文章就介绍到这了,更多相关SpringBoot事务回滚内容请搜索IT俱乐部以前的文章或继续浏览下面的相关文章希望大家以后多多支持IT俱乐部!