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

        

         

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

                      

                              

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值