springboot 单元测试事务回滚


springboot 单元测试事务回滚

                               

应用:单元测试时,对数据库数据操作后可回滚

                                     

*********************

示例

                         

**************

配置文件

               

application.yml

spring:
  datasource:
    druid:
      url: jdbc:mysql://localhost:3306/h3?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=GMT%2B8&allowPublicKeyRetrieval=true
      driver-class-name: com.mysql.cj.jdbc.Driver
      username: root
      password: 123456

mybatis-plus:
  mapper-locations: classpath:/mappers/*.xml
  type-aliases-package: com.example.demo.pojo

                          

**************

pojo 层

                 

Person

@Getter
@Setter
@ApiModel(value = "Person对象", description = "")
public class Person extends Model<Person> {

    private static final long serialVersionUID = 1L;

    @TableId(value = "id", type = IdType.AUTO)
    private Integer id;

    private String name;

    private Integer age;


    @Override
    public Serializable pkVal() {
        return this.id;
    }

}

                    

**************

dao 层

       

PersonMapper

public interface PersonMapper extends BaseMapper<Person> {

}

                           

**************

service 层

       

PersonService

public interface PersonService extends IService<Person> {

}

                          

**************

service.impl 层

     

PersonServiceImpl

@Service
public class PersonServiceImpl extends ServiceImpl<PersonMapper, Person> implements PersonService {

}

                        

**************

controller 层

     

PersonController

@RestController
@RequestMapping("/person")
public class PersonController {

}

                         

**************

单元测试类

     

                        

                       

PersonServiceImplTest

@SpringBootTest
class PersonServiceImplTest {

    @Resource
    private PersonService personService;

    @Test
    public void test(){
        Person person=new Person();
        person.setId(1);
        person.setName("瓜田李下");
        person.setAge(20);

        personService.save(person);
    }

    @Test
    @Transactional
    public void test2(){
        Person person=new Person();
        person.setId(2);
        person.setName("海贼王");
        person.setAge(20);

        personService.save(person);
    }

    @Test
    @Transactional
    @Rollback(value = false)
    public void test3(){
        Person person=new Person();
        person.setId(3);
        person.setName("海贼王");
        person.setAge(20);

        personService.save(person);
    }
}

                           

                                 

*********************

使用测试

                   

test

        

         

测试通过,并且测试数据插入到数据库中 

                       

test2

        

         

测试通过,测试数据没有插入到数据库中 

                       

test3

        

         

测试通过,测试数据插入到数据库中 

                      

                              

おすすめ

転載: blog.csdn.net/weixin_43931625/article/details/120659302