詳解JUnit5參數(shù)化測試的幾種方式
參數(shù)化測試一直是津津樂道的話題,我們都知道JMeter有四種參數(shù)化方式:用戶自定義變量、用戶參數(shù)、CSV文件、函數(shù)助手,那么JUnit5有哪些參數(shù)化測試的方式呢?
依賴
JUnit5需要添加junit-jupiter-params依賴才能使用參數(shù)化:
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-params</artifactId>
<version>5.7.2</version>
<scope>test</scope>
</dependency>
簡單示例
@ParameterizedTest用來定義參數(shù)化測試,@ValueSource用來定義參數(shù)值:
@ParameterizedTest
@ValueSource(strings = { "racecar", "radar", "able was I ere I saw elba" })
void palindromes(String candidate) {
assertTrue(StringUtils.isPalindrome(candidate));
}
執(zhí)行結(jié)果:
palindromes(String) ✔ ├─ [1] candidate=racecar ✔ ├─ [2] candidate=radar ✔ └─ [3] candidate=able was I ere I saw elba ✔
參數(shù)值會(huì)匹配測試方法的參數(shù)列表,然后依次賦值,這里一共產(chǎn)生了3個(gè)測試。
七種方式
1 @ValueSource
@ValueSource是最簡單的參數(shù)化方式,它是一個(gè)數(shù)組,支持以下數(shù)據(jù)類型:
- short
- byte
- int
- long
- float
- double
- char
- boolean
- java.lang.String
- java.lang.Class
示例:
@ParameterizedTest
@ValueSource(ints = { 1, 2, 3 })
void testWithValueSource(int argument) {
assertTrue(argument > 0 && argument < 4);
}
2 Null and Empty Sources
@NullSource 值為null
不能用在基元類型的測試方法。
@EmptySource 值為空,根據(jù)測試方法的參數(shù)類決定數(shù)據(jù)類型,支持java.lang.String, java.util.List, java.util.Set, java.util.Map, 基元類型數(shù)組 (int[], char[][]等), 對象數(shù)組 (String[], Integer[][]等)
@NullAndEmptySource 結(jié)合了前面兩個(gè)
示例:
@ParameterizedTest
@NullSource
@EmptySource
@ValueSource(strings = { " ", " ", "\t", "\n" })
void nullEmptyAndBlankStrings(String text) {
assertTrue(text == null || text.trim().isEmpty());
}
等價(jià)于:
@ParameterizedTest
@NullAndEmptySource
@ValueSource(strings = { " ", " ", "\t", "\n" })
void nullEmptyAndBlankStrings(String text) {
assertTrue(text == null || text.trim().isEmpty());
}
3 @EnumSource
參數(shù)化的值為枚舉類型。
示例:
@ParameterizedTest
@EnumSource
void testWithEnumSourceWithAutoDetection(ChronoUnit unit) {
assertNotNull(unit);
}
其中的ChronoUnit是個(gè)日期枚舉類。
ChronoUnit是接口TemporalUnit的實(shí)現(xiàn)類,如果測試方法的參數(shù)為TemporalUnit,那么需要給@EnumSource加上值:
@ParameterizedTest
@EnumSource(ChronoUnit.class)
void testWithEnumSource(TemporalUnit unit) {
assertNotNull(unit);
}
因?yàn)镴Unit5規(guī)定了@EnumSource的默認(rèn)值的類型必須是枚舉類型。
names屬性用來指定使用哪些特定的枚舉值:
@ParameterizedTest
@EnumSource(names = { "DAYS", "HOURS" })
void testWithEnumSourceInclude(ChronoUnit unit) {
assertTrue(EnumSet.of(ChronoUnit.DAYS, ChronoUnit.HOURS).contains(unit));
}
mode屬性用來指定使用模式,比如排除哪些枚舉值:
@ParameterizedTest
@EnumSource(mode = EXCLUDE, names = { "ERAS", "FOREVER" })
void testWithEnumSourceExclude(ChronoUnit unit) {
assertFalse(EnumSet.of(ChronoUnit.ERAS, ChronoUnit.FOREVER).contains(unit));
}
比如采用正則匹配:
@ParameterizedTest
@EnumSource(mode = MATCH_ALL, names = "^.*DAYS$")
void testWithEnumSourceRegex(ChronoUnit unit) {
assertTrue(unit.name().endsWith("DAYS"));
}
4 @MethodSource
參數(shù)值為factory方法,并且factory方法不能帶參數(shù)。
示例:
@ParameterizedTest
@MethodSource("stringProvider")
void testWithExplicitLocalMethodSource(String argument) {
assertNotNull(argument);
}
static Stream<String> stringProvider() {
return Stream.of("apple", "banana");
}
除非是@TestInstance(Lifecycle.PER_CLASS)生命周期,否則factory方法必須是static。factory方法的返回值是能轉(zhuǎn)換為Stream的類型,比如Stream, DoubleStream, LongStream, IntStream, Collection, Iterator, Iterable, 對象數(shù)組, 或者基元類型數(shù)組,比如:
@ParameterizedTest
@MethodSource("range")
void testWithRangeMethodSource(int argument) {
assertNotEquals(9, argument);
}
static IntStream range() {
return IntStream.range(0, 20).skip(10);
}
@MethodSource的屬性如果省略了,那么JUnit Jupiter會(huì)找跟測試方法同名的factory方法,比如:
@ParameterizedTest
@MethodSource
void testWithDefaultLocalMethodSource(String argument) {
assertNotNull(argument);
}
static Stream<String> testWithDefaultLocalMethodSource() {
return Stream.of("apple", "banana");
}
如果測試方法有多個(gè)參數(shù),那么factory方法也應(yīng)該返回多個(gè):
@ParameterizedTest
@MethodSource("stringIntAndListProvider")
void testWithMultiArgMethodSource(String str, int num, List<String> list) {
assertEquals(5, str.length());
assertTrue(num >=1 && num <=2);
assertEquals(2, list.size());
}
static Stream<Arguments> stringIntAndListProvider() {
return Stream.of(
arguments("apple", 1, Arrays.asList("a", "b")),
arguments("lemon", 2, Arrays.asList("x", "y"))
);
}
其中arguments(Object…)是Arguments接口的static factory method,也可以換成Arguments.of(Object…)。
factory方法也可以防止測試類外部:
package example;
import java.util.stream.Stream;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.MethodSource;
class ExternalMethodSourceDemo {
@ParameterizedTest
@MethodSource("example.StringsProviders#tinyStrings")
void testWithExternalMethodSource(String tinyString) {
// test with tiny string
}
}
class StringsProviders {
static Stream<String> tinyStrings() {
return Stream.of(".", "oo", "OOO");
}
}
5 @CsvSource
參數(shù)化的值為csv格式的數(shù)據(jù)(默認(rèn)逗號分隔),比如:
@ParameterizedTest
@CsvSource({
"apple, 1",
"banana, 2",
"'lemon, lime', 0xF1"
})
void testWithCsvSource(String fruit, int rank) {
assertNotNull(fruit);
assertNotEquals(0, rank);
}
delimiter屬性可以設(shè)置分隔字符。delimiterString屬性可以設(shè)置分隔字符串(String而非char)。
更多輸入輸出示例如下:

注意,如果null引用的目標(biāo)類型是基元類型,那么會(huì)報(bào)異常ArgumentConversionException。
6 @CsvFileSource
顧名思義,選擇本地csv文件作為數(shù)據(jù)來源。
示例:
@ParameterizedTest
@CsvFileSource(resources = "/two-column.csv", numLinesToSkip = 1)
void testWithCsvFileSourceFromClasspath(String country, int reference) {
assertNotNull(country);
assertNotEquals(0, reference);
}
@ParameterizedTest
@CsvFileSource(files = "src/test/resources/two-column.csv", numLinesToSkip = 1)
void testWithCsvFileSourceFromFile(String country, int reference) {
assertNotNull(country);
assertNotEquals(0, reference);
}
delimiter屬性可以設(shè)置分隔字符。delimiterString屬性可以設(shè)置分隔字符串(String而非char)。需要特別注意的是,#開頭的行會(huì)被認(rèn)為是注釋而略過。
7 @ArgumentsSource
自定義ArgumentsProvider。
示例:
@ParameterizedTest
@ArgumentsSource(MyArgumentsProvider.class)
void testWithArgumentsSource(String argument) {
assertNotNull(argument);
}
public class MyArgumentsProvider implements ArgumentsProvider {
@Override
public Stream<? extends Arguments> provideArguments(ExtensionContext context) {
return Stream.of("apple", "banana").map(Arguments::of);
}
}
MyArgumentsProvider必須是外部類或者static內(nèi)部類。
參數(shù)類型轉(zhuǎn)換
隱式轉(zhuǎn)換
JUnit Jupiter會(huì)對String類型進(jìn)行隱式轉(zhuǎn)換。比如:
@ParameterizedTest
@ValueSource(strings = "SECONDS")
void testWithImplicitArgumentConversion(ChronoUnit argument) {
assertNotNull(argument.name());
}
更多轉(zhuǎn)換示例:


也可以把String轉(zhuǎn)換為自定義對象:
@ParameterizedTest
@ValueSource(strings = "42 Cats")
void testWithImplicitFallbackArgumentConversion(Book book) {
assertEquals("42 Cats", book.getTitle());
}
public class Book {
private final String title;
private Book(String title) {
this.title = title;
}
public static Book fromTitle(String title) {
return new Book(title);
}
public String getTitle() {
return this.title;
}
}
JUnit Jupiter會(huì)找到Book.fromTitle(String)方法,然后把@ValueSource的值傳入進(jìn)去,進(jìn)而把String類型轉(zhuǎn)換為Book類型。轉(zhuǎn)換的factory方法既可以是接受單個(gè)String參數(shù)的構(gòu)造方法,也可以是接受單個(gè)String參數(shù)并返回目標(biāo)類型的普通方法。詳細(xì)規(guī)則如下(官方原文):

顯式轉(zhuǎn)換
顯式轉(zhuǎn)換需要使用@ConvertWith注解:
@ParameterizedTest
@EnumSource(ChronoUnit.class)
void testWithExplicitArgumentConversion(
@ConvertWith(ToStringArgumentConverter.class) String argument) {
assertNotNull(ChronoUnit.valueOf(argument));
}
并實(shí)現(xiàn)ArgumentConverter:
public class ToStringArgumentConverter extends SimpleArgumentConverter {
@Override
protected Object convert(Object source, Class<?> targetType) {
assertEquals(String.class, targetType, "Can only convert to String");
if (source instanceof Enum<?>) {
return ((Enum<?>) source).name();
}
return String.valueOf(source);
}
}
如果只是簡單類型轉(zhuǎn)換,實(shí)現(xiàn)TypedArgumentConverter即可:
public class ToLengthArgumentConverter extends TypedArgumentConverter<String, Integer> {
protected ToLengthArgumentConverter() {
super(String.class, Integer.class);
}
@Override
protected Integer convert(String source) {
return source.length();
}
}
JUnit Jupiter只內(nèi)置了一個(gè)JavaTimeArgumentConverter,通過@JavaTimeConversionPattern使用:
@ParameterizedTest
@ValueSource(strings = { "01.01.2017", "31.12.2017" })
void testWithExplicitJavaTimeConverter(
@JavaTimeConversionPattern("dd.MM.yyyy") LocalDate argument) {
assertEquals(2017, argument.getYear());
}
參數(shù)聚合
測試方法的多個(gè)參數(shù)可以聚合為一個(gè)ArgumentsAccessor參數(shù),然后通過get來取值,示例:
@ParameterizedTest
@CsvSource({
"Jane, Doe, F, 1990-05-20",
"John, Doe, M, 1990-10-22"
})
void testWithArgumentsAccessor(ArgumentsAccessor arguments) {
Person person = new Person(arguments.getString(0),
arguments.getString(1),
arguments.get(2, Gender.class),
arguments.get(3, LocalDate.class));
if (person.getFirstName().equals("Jane")) {
assertEquals(Gender.F, person.getGender());
}
else {
assertEquals(Gender.M, person.getGender());
}
assertEquals("Doe", person.getLastName());
assertEquals(1990, person.getDateOfBirth().getYear());
}
也可以自定義Aggregator:
public class PersonAggregator implements ArgumentsAggregator {
@Override
public Person aggregateArguments(ArgumentsAccessor arguments, ParameterContext context) {
return new Person(arguments.getString(0),
arguments.getString(1),
arguments.get(2, Gender.class),
arguments.get(3, LocalDate.class));
}
}
然后通過@AggregateWith來使用:
@ParameterizedTest
@CsvSource({
"Jane, Doe, F, 1990-05-20",
"John, Doe, M, 1990-10-22"
})
void testWithArgumentsAggregator(@AggregateWith(PersonAggregator.class) Person person) {
// perform assertions against person
}
借助于組合注解,我們可以進(jìn)一步簡化代碼:
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.PARAMETER)
@AggregateWith(PersonAggregator.class)
public @interface CsvToPerson {
}
@ParameterizedTest
@CsvSource({
"Jane, Doe, F, 1990-05-20",
"John, Doe, M, 1990-10-22"
})
void testWithCustomAggregatorAnnotation(@CsvToPerson Person person) {
// perform assertions against person
}
自定義顯示名字
參數(shù)化測試生成的test,JUnit Jupiter給定了默認(rèn)名字,我們可以通過name屬性進(jìn)行自定義。
示例:
@DisplayName("Display name of container")
@ParameterizedTest(name = "{index} ==> the rank of ''{0}'' is {1}")
@CsvSource({ "apple, 1", "banana, 2", "'lemon, lime', 3" })
void testWithCustomDisplayNames(String fruit, int rank) {
}
結(jié)果:
Display name of container ✔ ├─ 1 ==> the rank of 'apple' is 1 ✔ ├─ 2 ==> the rank of 'banana' is 2 ✔ └─ 3 ==> the rank of 'lemon, lime' is 3 ✔
注意如果要顯示'apple',需要使用兩層''apple'',因?yàn)閚ame是MessageFormat。
占位符說明如下:

小結(jié)
本文介紹了JUnit5參數(shù)化測試的7種方式,分別是@ValueSource,Null and Empty Sources,@EnumSource,@MethodSource,@CsvSource,@CsvFileSource,@ArgumentsSource,比較偏向于Java語法,符合JUnit單元測試框架的特征。另外還介紹了JUnit Jupiter的參數(shù)類型轉(zhuǎn)換和參數(shù)聚合。最后,如果想要自定義參數(shù)化測試的名字,可以使用name屬性實(shí)現(xiàn)。
參考資料:
https://junit.org/junit5/docs/current/user-guide/#writing-tests-parameterized-tests
到此這篇關(guān)于詳解JUnit5參數(shù)化測試的幾種方式的文章就介紹到這了,更多相關(guān)JUnit5參數(shù)化測試 內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
spring通過構(gòu)造函數(shù)注入實(shí)現(xiàn)方法分析
這篇文章主要介紹了spring通過構(gòu)造函數(shù)注入實(shí)現(xiàn)方法,結(jié)合實(shí)例形式分析了spring通過構(gòu)造函數(shù)注入的原理、實(shí)現(xiàn)步驟及相關(guān)操作注意事項(xiàng),需要的朋友可以參考下2019-10-10
java編程之單元測試(Junit)實(shí)例分析(附實(shí)例源碼)
這篇文章主要介紹了java編程之單元測試(Junit),結(jié)合實(shí)例形式較為詳細(xì)的分析總結(jié)了Java單元測試的原理、步驟及相關(guān)注意事項(xiàng),并附帶了完整代碼供讀者下載參考,需要的朋友可以參考下2015-11-11
淺談java實(shí)現(xiàn)背包算法(0-1背包問題)
本篇文章主要介紹了淺談java實(shí)現(xiàn)背包算法(0-1背包問題) ,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-08-08
java中MultipartFile類型轉(zhuǎn)為File類型的4種方法
Spring提供了一個(gè)MultipartFile接口來處理文件上傳,但有時(shí)候我們需要將MultipartFile轉(zhuǎn)換為File來進(jìn)行一些特定的操作,比如保存文件到本地或者進(jìn)行文件的處理等,這篇文章主要給大家介紹了關(guān)于java中MultipartFile類型轉(zhuǎn)為File類型的4種方法,需要的朋友可以參考下2024-09-09
SpringBoot可視化監(jiān)控的具體應(yīng)用
最近越發(fā)覺得,任何一個(gè)系統(tǒng)上線,運(yùn)維監(jiān)控都太重要了,本文介紹了SpringBoot可視化監(jiān)控的具體應(yīng)用,分享給大家,有興趣的同學(xué)可以參考一下2021-06-06
通過Spring Boot + Mybatis + Redis快速搭建現(xiàn)代化Web項(xiàng)目
本篇文章介紹了如何通過Spring Boot、Mybatis以及Redis快速搭建一個(gè)現(xiàn)代化的Web項(xiàng)目,并且同時(shí)介紹了如何在Spring Boot下優(yōu)雅地書寫單元測試來保證我們的代碼質(zhì)量。具體內(nèi)容詳情大家通過本文學(xué)習(xí)下吧2017-12-12

