这里分类和汇总了欣宸的全部原创(含配套源码):https://github.com/zq2599/blog_demos
《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);
}
}
<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" })
public enum Types {
SMALL,
BIG,
UNKNOWN
}
@Order(6)
@DisplayName("多个枚举型入参")
@ParameterizedTest
@EnumSource
void enumSourceTest(Types type) {
log.info("enumSourceTest [{}]", type);
}
@EnumSource(names={"SMALL", "UNKNOWN"})
@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