https://github.com/zq2599/blog_demosjava
内容:全部原创文章分类汇总及配套源码,涉及Java、Docker、Kubernetes、DevOPS等;git
《JUnit5学习》系列旨在经过实战提高SpringBoot环境下的单元测试技能,一共八篇文章,连接以下:程序员
名称 | 连接 | 备注 |
---|---|---|
项目主页 | https://github.com/zq2599/blog_demos | 该项目在GitHub上的主页 |
git仓库地址(https) | https://github.com/zq2599/blog_demos.git | 该项目源码的仓库地址,https协议 |
git仓库地址(ssh) | git@github.com:zq2599/blog_demos.git | 该项目源码的仓库地址,ssh协议 |
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>com.bolingcavalry</groupId> <artifactId>junitpractice</artifactId> <version>1.0-SNAPSHOT</version> <relativePath>../pom.xml</relativePath> </parent> <groupId>com.bolingcavalry</groupId> <artifactId>parameterized</artifactId> <version>0.0.1-SNAPSHOT</version> <name>parameterized</name> <description>Demo project for parameterized expirence in Spring Boot junit</description> <properties> <java.version>1.8</java.version> </properties> <dependencyManagement> <dependencies> <dependency> <groupId>org.junit</groupId> <artifactId>junit-bom</artifactId> <version>5.7.0</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> <exclusions> <exclusion> <groupId>org.junit.jupiter</groupId> <artifactId>junit-jupiter</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>org.junit.jupiter</groupId> <artifactId>junit-jupiter</artifactId> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
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); } }
5. 从上图可见执行参数化测试须要两步:首先用@ParameterizedTest取代@Test,表名此方法要执行参数化测试,而后用@ValueSource指定每次测试时的参数来自字符串类型的数组:{ "a", "b", "c" },每一个元素执行一次;
6. 至此,我们已体验过最简单的参数化测试,可见就是想办法使一个测试方法屡次执行,每次都用不一样的参数,接下来有关参数化测试的更多配置和规则将配合实战编码逐个展开,一块儿来体验吧;github
<dependencyManagement> <dependencies> <dependency> <groupId>org.junit</groupId> <artifactId>junit-bom</artifactId> <version>5.7.0</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement>
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> <exclusions> <exclusion> <groupId>org.junit.jupiter</groupId> <artifactId>junit-jupiter</artifactId> </exclusion> </exclusions> </dependency>
<dependency> <groupId>org.junit.jupiter</groupId> <artifactId>junit-jupiter</artifactId> <scope>test</scope> </dependency>
short byte int long float double char boolean java.lang.String java.lang.Class
@Order(2) @DisplayName("多个int型入参") @ParameterizedTest @ValueSource(ints = { 1,2,3 }) void intsTest(int candidate) { log.info("ints [{}]", candidate); assertTrue(candidate<3); }
@ValueSource(strings = { null, "a", "b", "c" })
@NullSource @ValueSource(strings = { "a", "b", "c" })
4. 与@NullSource表明null入参相似,@EmptySource表明空字符串入参,用法和执行结果以下图所示:web
5. 若是想同时用null和空字符串作测试方法的入参,可使用@NullAndEmptySource,用法和执行结果以下图所示:spring
public enum Types { SMALL, BIG, UNKNOWN }
@Order(6) @DisplayName("多个枚举型入参") @ParameterizedTest @EnumSource void enumSourceTest(Types type) { log.info("enumSourceTest [{}]", type); }
5. 若是不想执行枚举的全部值,而只要其中一部分,能够在name属性中指定:数据库
@EnumSource(names={"SMALL", "UNKNOWN"})
7. 也能够指定哪些值不被执行,此时要添加mode属性并设置为EXCLUDE(mode属性若是不写,默认值是INCLUDE,前面的例子中就是默认值):apache
@EnumSource(mode= EnumSource.Mode.EXCLUDE, names={"SMALL", "UNKNOWN"})
static Stream<String> stringProvider() { return Stream.of("apple1", "banana1"); }
@Order(9) @DisplayName("静态方法返回集合,用此集合中每一个元素做为入参") @ParameterizedTest @MethodSource("stringProvider") void methodSourceTest(String candidate) { log.info("methodSourceTest [{}]", candidate); }
@Order(10) @DisplayName("静态方法返回集合,该静态方法在另外一个类中") @ParameterizedTest @MethodSource("com.bolingcavalry.parameterized.service.impl.Utils#getStringStream") void methodSourceFromOtherClassTest(String candidate) { log.info("methodSourceFromOtherClassTest [{}]", candidate); }
static Stream<String> methodSourceWithoutMethodNameTest() { return Stream.of("apple3", "banana3"); } @Order(11) @DisplayName("静态方法返回集合,不指定静态方法名,自动匹配") @ParameterizedTest @MethodSource void methodSourceWithoutMethodNameTest(String candidate) { log.info("methodSourceWithoutMethodNameTest [{}]", candidate); }
@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); }
@Order(13) @DisplayName("CSV格式多条记录入参(识别null)") @ParameterizedTest @CsvSource(value = { "apple2, 21", "banana2, 22", "'lemon2, lime2', 0x0A", "NIL, 3" }, nullValues = "NIL" ) void csvSourceWillNullTokenTest(String fruit, int rank) { log.info("csvSourceWillNullTokenTest, fruit [{}], rank [{}]", fruit, rank); }
@Order(14) @DisplayName("CSV文件多条记录入参") @ParameterizedTest @CsvFileSource(files = "src/test/resources/two-column.csv", numLinesToSkip = 1) void csvFileTest(String country, int reference) { log.info("csvSourceTest, country [{}], reference [{}]", country, reference); }
Country, reference Sweden, 1 Poland, 2 "United States of America", 3
微信搜索「程序员欣宸」,我是欣宸,期待与您一同畅游Java世界...
https://github.com/zq2599/blog_demosapi