本文共 6276 字,大约阅读时间需要 20 分钟。
欢迎访问我的GitHub
《JUnit5学习》系列旨在通过实战提升SpringBoot环境下的单元测试技能,共计八篇文章,链接如下:
[系列文章链接待补充]
本文是《JUnit5学习》系列的第六篇,主要介绍参数化测试(Parameterized Tests)的基础知识,包括以下内容:
源码下载
项目主页:[链接待补充]
git仓库地址(HTTPS):[链接待补充]
git仓库地址(SSH):git@github.com:zq2599/blog_demos.git
在父工程junitpractice中新建名为parameterized的子工程,其pom.xml内容如下:
4.0.0 com.bolingcavalry junitpractice 1.0-SNAPSHOT ../pom.xml com.bolingcavalry parameterized 0.0.1-SNAPSHOT parameterized Demo project for parameterized experience in Spring Boot junit 1.8 org.junit junit-bom 5.7.0 pom import org.springframework.boot spring-boot-starter-web org.projectlombok lombok org.springframework.boot spring-boot-starter-test test org.junit.jupiter junit-jupiter org.junit.jupiter junit-jupiter test org.springframework.boot spring-boot-maven-plugin
新建测试类HelloTest.java,其路径为junitpractice\parameterized\src\test\java\com\bolingcavalry\parameterized\service\impl,内容如下:
package com.bolingcavalry.parameterized.service.impl;import lombok.extern.slf4j.Slf4j;import org.junit.jupiter.api.DisplayName;import org.junit.jupiter.api.MethodOrderer;import org.junit.jupiter.api.Order;import org.junit.jupiter.api.TestMethodOrder;import org.junit.jupiter.params.ParameterizedTest;import org.junit.jupiter.params.provider.ValueSource;import org.springframework.boot.test.context.SpringBootTest;import static org.junit.jupiter.api.Assertions.assertTrue;@SpringBootTest@Slf4j@TestMethodOrder(MethodOrderer.OrderAnnotation.class)public class HelloTest { @Order(1) @DisplayName("多个字符串型入参") @ParameterizedTest @ValueSource(strings = { "a", "b", "c" }) void stringsTest(String candidate) { log.info("stringsTest [{}]", candidate); assertTrue(null != candidate); }} 在pom.xml中执行以下操作:
org.junit junit-bom 5.7.0 pom import
org.springframework.boot spring-boot-starter-test test org.junit.jupiter junit-jupiter
org.junit.jupiter junit-jupiter test
ValueSource是最简单常用的数据源,支持以下类型的数组:
例如:
@Order(2)@DisplayName("多个int型入参")@ParameterizedTest@ValueSource(ints = { 1, 2, 3 })void intsTest(int candidate) { log.info("ints [{}]", candidate); assertTrue(candidate < 3);} 执行结果如下:
在用字符串作为入参时,通常要考虑入参为null的情况。可以使用以下方式:
@ValueSource(strings = { null, "a", "b", "c" }) 或者使用@NullSource注解:
@NullSource@ValueSource(strings = { "a", "b", "c" }) 执行结果如下:
EnumSource可以让一个枚举类中的全部或者部分值作为测试方法的入参。创建枚举类Types.java:
public enum Types { SMALL, BIG, UNKNOWN} 测试方法:
@Order(6)@DisplayName("多个枚举型入参")@ParameterizedTest@EnumSourcevoid enumSourceTest(Types type) { log.info("enumSourceTest [{}]", type);} 执行结果如下:
@MethodSource可以指定一个方法名称,该方法返回的元素集合作为测试方法的入参。定义一个静态方法:
static StreamstringProvider() { return Stream.of("apple1", "banana1");}
测试方法:
@Order(9)@DisplayName("静态方法返回集合,用此集合中每个元素作为入参")@ParameterizedTest@MethodSource("stringProvider")void methodSourceTest(String candidate) { log.info("methodSourceTest [{}]", candidate);} 或者在同一个类中不指定方法名,JUnit会自动匹配:
static StreammethodSourceWithoutMethodNameTest() { return Stream.of("apple3", "banana3");}
测试方法:
@Order(11)@DisplayName("静态方法返回集合,不指定静态方法名,自动匹配")@ParameterizedTest@MethodSourcevoid methodSourceWithoutMethodNameTest(String candidate) { log.info("methodSourceWithoutMethodNameTest [{}]", candidate);} @CsvSource解决了测试方法入参有多个字段的问题。例如:
@Order(12)@DisplayName("CSV格式多条记录入参")@ParameterizedTest@CsvSource({ "apple1, 11", "banana1, 12", "'lemon1, lime1', 0x0A"})void csvSourceTest(String fruit, int rank) { log.info("csvSourceTest, fruit [{}], rank [{}]", fruit, rank);} 执行结果如下:
@CsvFileSource用于指定CSV文件作为数据源。创建文件two-column.csv:
Country, referenceSweden, 1Poland, 2"United States of America", 3
测试方法:
@Order(14)@DisplayName("CSV文件多条记录入参")@ParameterizedTest@CsvFileSource(files = "src/test/resources/two-column.csv", numLinesToSkip = 1)void csvFileTest(String country, int reference) { log.info("csvFileTest, country [{}], reference [{}]", country, reference);} 至此,我们对JUnit5的参数化测试(Parameterized Tests)有了基本的了解。下一篇将深入探讨更高级的功能。
转载地址:http://khtkz.baihongyu.com/