@NullSource provides a single null value to a @ParameterizedTest.
@NullSource
null
@ParameterizedTest
package org.junit.jupiter.params.provider; ........ @Target({ElementType.ANNOTATION_TYPE, ElementType.METHOD}) @Retention(RetentionPolicy.RUNTIME) @Documented @API(status = STABLE, since = "5.7") @ArgumentsSource(NullArgumentsProvider.class) public @interface NullSource {}
package com.logicbig.example; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.NullSource; import static org.junit.jupiter.api.Assertions.assertNull; public class ParameterizedNullSourceTest { @ParameterizedTest(name = "#{index} null argument should be provided") @NullSource void receivesNull(String input) { assertNull(input); } }
mvn test -Dtest=ParameterizedNullSourceTest
D:\example-projects\junit-5\junit-5-parameterized-tests\junit-5-parameterized-nullsource>mvn test -Dtest=ParameterizedNullSourceTest[INFO] Scanning for projects...[INFO] [INFO] -------< com.logicbig.example:junit-5-parameterized-nullsource >--------[INFO] Building junit-5-parameterized-nullsource 1.0-SNAPSHOT[INFO] from pom.xml[INFO] --------------------------------[ jar ]---------------------------------[INFO] [INFO] --- resources:3.3.1:resources (default-resources) @ junit-5-parameterized-nullsource ---[INFO] skip non existing resourceDirectory D:\example-projects\junit-5\junit-5-parameterized-tests\junit-5-parameterized-nullsource\src\main\resources[INFO] [INFO] --- compiler:3.14.1:compile (default-compile) @ junit-5-parameterized-nullsource ---[INFO] No sources to compile[INFO] [INFO] --- resources:3.3.1:testResources (default-testResources) @ junit-5-parameterized-nullsource ---[INFO] skip non existing resourceDirectory D:\example-projects\junit-5\junit-5-parameterized-tests\junit-5-parameterized-nullsource\src\test\resources[INFO] [INFO] --- compiler:3.14.1:testCompile (default-testCompile) @ junit-5-parameterized-nullsource ---[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-nullsource ---[INFO] Using auto detected provider org.apache.maven.surefire.junitplatform.JUnitPlatformProvider[INFO] [INFO] -------------------------------------------------------[INFO] T E S T S[INFO] -------------------------------------------------------[INFO] +--com.logicbig.example.ParameterizedNullSourceTest - 0.125 ss[INFO] | '-- [OK] receivesNull(String) #1 null argument should be provided - 0.037 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.972 s[INFO] Finished at: 2025-12-04T15:29:33+08:00[INFO] ------------------------------------------------------------------------
Dependencies and Technologies Used:
Version compatibilities of junit-jupiter-engine with this example:
Versions in green have been tested.