【SpringBoot】 设置随机数据 用于测试用例

在这里插入图片描述

个人简介:Java领域新星创作者;阿里云技术博主、星级博主、专家博主;正在Java学习的路上摸爬滚打,记录学习的过程~
个人主页:.29.的博客
学习社区:进去逛一逛~

在这里插入图片描述

设置随机数据——常用于测试用例

SpringBoot设置随机数据


  • 测试用例常常采用随机值进行测试,可以在SpringBoot配置文件中设置随机数据

yml配置文件

testcase:
  book:
    id: ${
    
    random.int}
    type: ${
    
    random.value}
    name: ${
    
    random.uuid}
    description: ${
    
    random.long}

实体类中注入配置文件设置的随机数据

/**
 * @author .29.
 * @create 2023-04-02 10:45
 */
@Component
@Data
@ConfigurationProperties(prefix = "testcase.book")
public class BookCase {
    
    
    private int id;
    private String type;
    private String name;
    private String description;
}


测试用例

/**
 * @author .29.
 * @create 2023-04-02 10:50
 */
@SpringBootTest
public class testRandom {
    
    
    @Autowired
    private BookCase bookCase;

    @Test
    void random(){
    
    
        System.out.println(bookCase);
    }
}

在这里插入图片描述



可设置的随机数据 详解:

  • ${random.int} —— 随机整数
  • ${random.int(10)} —— 10以内的随机整数
  • ${random.int(10,20)} —— 10~20的随机整数
  • ${random.uuid} —— 随机uuid
  • ${random.value} —— 随机字符串,MD5字符串,32位
  • ${random.long} —— 随机整数(long范围内)
testcase:
  book: 
    id: ${
    
    random.int}            # 随机整数
    id2: ${
    
    random.int(10)}       # 10以内的随机整数
    type: ${
    
    random.int(10,20)}  # 10~20的随机整数
    uuid: ${
    
    random.uuid}         # 随机uuid
    name: ${
    
    random.value}        # 随机字符串,MD5字符串,32位
    long: ${
    
    random.long}         # 随机整数(long范围内)


在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/ebb29bbe/article/details/131066957