本文共 9212 字,大约阅读时间需要 30 分钟。
内容:所有原创文章分类汇总及配套源码,涉及Java、Docker、Kubernetes、DevOPS等;
《JUnit5学习》系列旨在通过实战提升SpringBoot环境下的单元测试技能,一共八篇文章,链接如下:
名称 | 链接 | 备注 |
---|---|---|
项目主页 | 该项目在GitHub上的主页 | |
git仓库地址(https) | 该项目源码的仓库地址,https协议 | |
git仓库地址(ssh) | git@github.com:zq2599/blog_demos.git | 该项目源码的仓库地址,ssh协议 |
4.0.0 com.bolingcavalry junitpractice 1.0-SNAPSHOT ../pom.xml com.bolingcavalry parameterized 0.0.1-SNAPSHOT parameterized Demo project for parameterized expirence 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
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); }}
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
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 StreamstringProvider() { 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 StreammethodSourceWithoutMethodNameTest() { 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, referenceSweden, 1Poland, 2"United States of America", 3
微信搜索「程序员欣宸」,我是欣宸,期待与您一同畅游Java世界...
转载地址:http://khtkz.baihongyu.com/