@ValueSource provides a simple way to feed literal values to a @ParameterizedTest.
Java source and doc
Definition of ValueSource(Version: junit-5 5.8.2) package org.junit.jupiter.params.provider;
........
@Target({ElementType.ANNOTATION_TYPE, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@API(status = STABLE, since = "5.7")
@ArgumentsSource(ValueArgumentsProvider.class)
public @interface ValueSource {
short[] shorts() default {}; 1
byte[] bytes() default {}; 2
int[] ints() default {}; 3
long[] longs() default {}; 4
float[] floats() default {}; 5
double[] doubles() default {}; 6
char[] chars() default {}; 7
boolean[] booleans() default {}; 8
String[] strings() default {}; 9
Class<?>[] classes() default {}; 10
}
Example
package com.logicbig.example;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
import static org.junit.jupiter.api.Assertions.assertTrue;
public class ParameterizedValueSourceTest {
@ParameterizedTest(name = "run #{index} with argument={0}")
@ValueSource(strings = {"madam", "racecar", "level", "radar"})
void palindromeShouldBeTrue(String candidate) {
assertTrue(isPalindrome(candidate));
}
private static boolean isPalindrome(String s) {
int i = 0, j = s.length() - 1;
while (i < j) {
if (s.charAt(i++) != s.charAt(j--)) {
return false;
}
}
return true;
}
@ParameterizedTest
@ValueSource(ints = {1, 2, 3, 5, 8, 13, 21})
void testIsPositive(int number) {
assertTrue(number > 0);
}
@ParameterizedTest
@ValueSource(strings = {"", " ", "\t", "\n"})
void testIsBlank(String input) {
assertTrue(input.trim().isEmpty());
}
}
mvn test -Dtest=ParameterizedValueSourceTest OutputD:\example-projects\junit-5\junit-5-parameterized-tests\junit-5-parameterized-valuesource>mvn test -Dtest=ParameterizedValueSourceTest [INFO] Scanning for projects... [INFO] [INFO] -------< com.logicbig.example:junit-5-parameterized-valuesource >------- [INFO] Building junit-5-parameterized-valuesource 1.0-SNAPSHOT [INFO] from pom.xml [INFO] --------------------------------[ jar ]--------------------------------- [INFO] [INFO] --- resources:3.3.1:resources (default-resources) @ junit-5-parameterized-valuesource --- [INFO] skip non existing resourceDirectory D:\example-projects\junit-5\junit-5-parameterized-tests\junit-5-parameterized-valuesource\src\main\resources [INFO] [INFO] --- compiler:3.14.1:compile (default-compile) @ junit-5-parameterized-valuesource --- [INFO] No sources to compile [INFO] [INFO] --- resources:3.3.1:testResources (default-testResources) @ junit-5-parameterized-valuesource --- [INFO] skip non existing resourceDirectory D:\example-projects\junit-5\junit-5-parameterized-tests\junit-5-parameterized-valuesource\src\test\resources [INFO] [INFO] --- compiler:3.14.1:testCompile (default-testCompile) @ junit-5-parameterized-valuesource --- [INFO] Nothing to compile - all classes are up to date. [INFO] [INFO] --- surefire:3.5.0:test (default-test) @ junit-5-parameterized-valuesource --- [INFO] Using auto detected provider org.apache.maven.surefire.junitplatform.JUnitPlatformProvider [INFO] [INFO] ------------------------------------------------------- [INFO] T E S T S [INFO] ------------------------------------------------------- [INFO] +--com.logicbig.example.ParameterizedValueSourceTest - 0.203 ss [INFO] | +-- [OK] testIsPositive(int)[1] 1 - 0.037 ss [INFO] | +-- [OK] testIsPositive(int)[2] 2 - 0.001 ss [INFO] | +-- [OK] testIsPositive(int)[3] 3 - 0.001 ss [INFO] | +-- [OK] testIsPositive(int)[4] 5 - 0.002 ss [INFO] | +-- [OK] testIsPositive(int)[5] 8 - 0.001 ss [INFO] | +-- [OK] testIsPositive(int)[6] 13 - 0.002 ss [INFO] | +-- [OK] testIsPositive(int)[7] 21 - 0.001 ss [INFO] | +-- [OK] palindromeShouldBeTrue(String) run #1 with argument="madam" - 0.004 ss [INFO] | +-- [OK] palindromeShouldBeTrue(String) run #2 with argument="racecar" - 0.001 ss [INFO] | +-- [OK] palindromeShouldBeTrue(String) run #3 with argument="level" - 0.001 ss [INFO] | +-- [OK] palindromeShouldBeTrue(String) run #4 with argument="radar" - 0.002 ss [INFO] | +-- [OK] testIsBlank(String)[1] "" - 0.002 ss [INFO] | +-- [OK] testIsBlank(String)[2] " " - 0.002 ss [INFO] | +-- [OK] testIsBlank(String)[3] "\t" - 0.003 ss [INFO] | '-- [OK] testIsBlank(String)[4] "\n" - 0.001 ss [INFO] [INFO] Results: [INFO] [INFO] Tests run: 15, Failures: 0, Errors: 0, Skipped: 0 [INFO] [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 3.105 s [INFO] Finished at: 2025-12-04T19:43:07+08:00 [INFO] ------------------------------------------------------------------------
Example ProjectDependencies and Technologies Used: - junit-jupiter-engine 6.0.1 (Module "junit-jupiter-engine" of JUnit)
Version Compatibility: 5.0.0 - 6.0.1 Version compatibilities of junit-jupiter-engine with this example:
- 5.0.0
- 5.0.1
- 5.0.2
- 5.0.3
- 5.1.0
- 5.1.1
- 5.2.0
- 5.3.0
- 5.3.1
- 5.3.2
- 5.4.0
- 5.4.1
- 5.4.2
- 5.5.0
- 5.5.1
- 5.5.2
- 5.6.0
- 5.6.1
- 5.6.2
- 5.6.3
- 5.7.0
- 5.7.1
- 5.7.2
- 5.8.0
- 5.8.1
- 5.8.2
- 5.9.0
- 5.9.1
- 5.9.2
- 5.9.3
- 5.10.0
- 5.10.1
- 5.10.2
- 5.10.3
- 5.10.4
- 5.10.5
- 5.11.0
- 5.11.1
- 5.11.2
- 5.11.3
- 5.11.4
- 5.12.0
- 5.12.1
- 5.12.2
- 5.13.0
- 5.13.1
- 5.13.2
- 5.13.3
- 5.13.4
- 5.14.0
- 5.14.1
- 6.0.0
- 6.0.1
Versions in green have been tested.
- junit-jupiter-params 6.0.1 (Module "junit-jupiter-params" of JUnit)
- JDK 25
- Maven 3.9.11
|