SpringBoot data layer test transaction rollback

Table of contents

Data layer test transaction rollback

down

pojo object

service

Test case data setting


Data layer test transaction rollback

pom.xml imports some corresponding coordinates, mysql, Mp, etc.

<dependency>

            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
            <version>3.5.2</version>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>

down

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<Person> {

}

pojo object

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

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<Person> {

  
}

serviceImpl



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

}

 Under the PersonServiceTest class

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));
    }
}

plus @Transactional runs

Running with @Transactional and @Rollback(false)

 To add a transaction to a test case, add @Transactional, SpringBoot will roll back the transaction submission operation corresponding to the test case, that is, springboot recognizes that this is a test, so it will not submit the transaction, but it will occupy the id. No data will be displayed.

If you want to submit a transaction in a test case, you can pass @Rollback(false), no rollback, the default value is true, adding false will not roll back, and the test data can be displayed in the database.

Test case data setting

Test case data is usually tested with random values, using the random digits provided by SpringBoot to assign values

 ${random.int} represents a random integer

${random.int(10)} means a random number within 10

${random.int(10,20)} represents a random number from 10 to 20

Among them, () can be any character, such as [ ], @@ can be used.

under configuration file

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

Define a class to receive 

@Data
@Component//给spring管理
@ConfigurationProperties(prefix = "personrandom")
public class  Person {
    private String  name;
    private String  age;
    private String  detail;
}

under test class 

@SpringBootTest
public class RandomTest {
@Autowired
    private Person person;
    @Test
    public void KC(){
        System.out.println(person);
    }
}

operation result

Guess you like

Origin blog.csdn.net/weixin_60719453/article/details/127423660