@EmptySource provides empty values (e.g., empty string) to a @ParameterizedTest.
@EmptySource
@ParameterizedTest
package org.junit.jupiter.params.provider; ........ @Target({ElementType.ANNOTATION_TYPE, ElementType.METHOD}) @Retention(RetentionPolicy.RUNTIME) @Documented @API(status = STABLE, since = "5.7") @ArgumentsSource(EmptyArgumentsProvider.class) public @interface EmptySource {}
package com.logicbig.example; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.EmptySource; import static org.junit.jupiter.api.Assertions.assertEquals; public class ParameterizedEmptySourceTest { @ParameterizedTest(name = "#{index} empty value provided -> '{0}'") @EmptySource void emptyStringHasLengthZero(String input) { // JUnit will pass an empty string for String parameter with @EmptySource assertEquals(0, input.length()); } }
mvn test -Dtest=ParameterizedEmptySourceTest
D:\example-projects\junit-5\junit-5-parameterized-tests\junit-5-parameterized-emptysource>mvn test -Dtest=ParameterizedEmptySourceTest[INFO] Scanning for projects...[INFO] [INFO] -------< com.logicbig.example:junit-5-parameterized-emptysource >-------[INFO] Building junit-5-parameterized-emptysource 1.0-SNAPSHOT[INFO] from pom.xml[INFO] --------------------------------[ jar ]---------------------------------[INFO] [INFO] --- resources:3.3.1:resources (default-resources) @ junit-5-parameterized-emptysource ---[INFO] skip non existing resourceDirectory D:\example-projects\junit-5\junit-5-parameterized-tests\junit-5-parameterized-emptysource\src\main\resources[INFO] [INFO] --- compiler:3.14.1:compile (default-compile) @ junit-5-parameterized-emptysource ---[INFO] No sources to compile[INFO] [INFO] --- resources:3.3.1:testResources (default-testResources) @ junit-5-parameterized-emptysource ---[INFO] skip non existing resourceDirectory D:\example-projects\junit-5\junit-5-parameterized-tests\junit-5-parameterized-emptysource\src\test\resources[INFO] [INFO] --- compiler:3.14.1:testCompile (default-testCompile) @ junit-5-parameterized-emptysource ---[INFO] Recompiling the module because of changed source code.[INFO] Compiling 1 source file with javac [debug target 25] to target\test-classes[INFO] [INFO] --- surefire:3.5.0:test (default-test) @ junit-5-parameterized-emptysource ---[INFO] Using auto detected provider org.apache.maven.surefire.junitplatform.JUnitPlatformProvider[INFO] [INFO] -------------------------------------------------------[INFO] T E S T S[INFO] -------------------------------------------------------[INFO] +--com.logicbig.example.ParameterizedEmptySourceTest - 0.136 ss[INFO] | '-- [OK] emptyStringHasLengthZero(String) #1 empty value provided -> {0} - 0.041 ss[INFO] [INFO] Results:[INFO] [INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0[INFO] [INFO] ------------------------------------------------------------------------[INFO] BUILD SUCCESS[INFO] ------------------------------------------------------------------------[INFO] Total time: 3.850 s[INFO] Finished at: 2025-12-04T15:33:43+08:00[INFO] ------------------------------------------------------------------------
Dependencies and Technologies Used:
Version compatibilities of junit-jupiter-engine with this example:
Versions in green have been tested.