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