• SpringBoot数据层测试事务回滚


    目录

    数据层测试事务回滚

    dao下

    pojo对象

    service

    测试用例数据设定


    数据层测试事务回滚

    pom.xml导入对应的一些坐标,mysql,Mp,等

    1. <dependency>
    2. <groupId>com.baomidougroupId>
    3. <artifactId>mybatis-plus-boot-starterartifactId>
    4. <version>3.5.2version>
    5. dependency>
    6. <dependency>
    7. <groupId>org.projectlombokgroupId>
    8. <artifactId>lombokartifactId>
    9. <optional>trueoptional>
    10. dependency>
    11. <dependency>
    12. <groupId>mysqlgroupId>
    13. <artifactId>mysql-connector-javaartifactId>
    14. <scope>runtimescope>
    15. dependency>

    dao下

    1. import com.baomidou.mybatisplus.core.mapper.BaseMapper;
    2. import com.pojo.Person;
    3. import org.apache.ibatis.annotations.Mapper;
    4. import org.mybatis.spring.annotation.MapperScan;
    5. import org.springframework.stereotype.Component;
    6. import org.springframework.stereotype.Repository;
    7. @Mapper//使用注解配置映射
    8. @Component//给spring管理,方便注入
    9. public interface PersonDao extends BaseMapper {
    10. }

    pojo对象

    1. package com.pojo;
    2. import com.baomidou.mybatisplus.annotation.IdType;
    3. import com.baomidou.mybatisplus.annotation.TableId;
    4. import com.baomidou.mybatisplus.annotation.TableName;
    5. import lombok.Data;
    6. @Data
    7. @TableName("tb_user")
    8. public class Person {
    9. private Long id;
    10. private String username;
    11. private String password;
    12. private String gender;
    13. private String addr;
    14. }

    service

    1. package com.service;
    2. import com.baomidou.mybatisplus.core.metadata.IPage;
    3. import com.baomidou.mybatisplus.extension.service.IService;
    4. import com.pojo.Person;
    5. public interface PersonService extends IService {
    6. }

    serviceImpl

    1. @Service
    2. public class PersonServiceImpl extends ServiceImpl implements PersonService {
    3. }

     PersonServiceTest类下

    1. package com.serviceTest;
    2. import com.pojo.Person;
    3. import com.service.PersonService;
    4. import org.junit.jupiter.api.Test;
    5. import org.springframework.beans.factory.annotation.Autowired;
    6. import org.springframework.boot.test.context.SpringBootTest;
    7. import org.springframework.test.annotation.Rollback;
    8. import org.springframework.transaction.annotation.Transactional;
    9. @SpringBootTest
    10. @Transactional
    11. @Rollback(false)
    12. public class PersonServiceTest {
    13. @Autowired
    14. private PersonService personService;
    15. @Test
    16. void testAdd(){
    17. Person person = new Person();
    18. person.setUsername("测试回滚2");
    19. person.setPassword("1");
    20. person.setGender("1");
    21. person.setAddr("1");
    22. System.out.println(personService.save(person));
    23. }
    24. }

    加上@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的随机数

    其中()可以是任意字符,如[ ],@@都可以。

    配置文件下

    1. personRandom:
    2. age: ${random.int(1,100)}
    3. name: ${random.value}
    4. detail: ${random.uuid}

    定义一个类接收 

    1. @Data
    2. @Component//给spring管理
    3. @ConfigurationProperties(prefix = "personrandom")
    4. public class Person {
    5. private String name;
    6. private String age;
    7. private String detail;
    8. }

    测试类下 

    1. @SpringBootTest
    2. public class RandomTest {
    3. @Autowired
    4. private Person person;
    5. @Test
    6. public void KC(){
    7. System.out.println(person);
    8. }
    9. }

    运行结果

  • 相关阅读:
    【网络教程】Iptables官方教程-学习笔记6-IPTABLES TARGETS
    【字符串与日期的相互转换NSDate Objective-C语言】
    [Hive] Map类型在表中是如何存储的
    Pikachu漏洞练习平台之XXE(XML外部实体注入)
    Object.keys的‘诡异’特性,你值得收藏!
    2021年雄安新区发展研究报告
    【Leetcode每日一题】「动态规划」1155.掷骰子等于目标和的方法数
    美创科技信创数据安全「利基者」!
    Latex语法学习09:如何编写一本书
    原理Redis-Dict字典
  • 原文地址:https://blog.csdn.net/weixin_60719453/article/details/127423660