JUnit Jupiter extension for parameterized tests
—
Simple and convenient argument providers for literal values, null values, and empty collections.
Provides arrays of literal values as test arguments, supporting all primitive types, strings, and classes.
/**
* Provides an array of literal values as arguments to parameterized tests
*/
@Target({ElementType.ANNOTATION_TYPE, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@API(status = STABLE, since = "5.0")
@ArgumentsSource(ValueArgumentsProvider.class)
@Repeatable(ValueSources.class)
@interface ValueSource {
/**
* Array of short values
*/
short[] shorts() default {};
/**
* Array of byte values
*/
byte[] bytes() default {};
/**
* Array of int values
*/
int[] ints() default {};
/**
* Array of long values
*/
long[] longs() default {};
/**
* Array of float values
*/
float[] floats() default {};
/**
* Array of double values
*/
double[] doubles() default {};
/**
* Array of char values
*/
char[] chars() default {};
/**
* Array of boolean values
*/
boolean[] booleans() default {};
/**
* Array of String values
*/
String[] strings() default {};
/**
* Array of Class values
*/
Class<?>[] classes() default {};
}Usage Examples:
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
class ValueSourceExamples {
@ParameterizedTest
@ValueSource(ints = {1, 2, 3, 5, 8})
void testWithIntegers(int value) {
assertTrue(value > 0);
assertTrue(value < 10);
}
@ParameterizedTest
@ValueSource(strings = {"apple", "banana", "cherry"})
void testWithStrings(String fruit) {
assertNotNull(fruit);
assertTrue(fruit.length() > 3);
}
@ParameterizedTest
@ValueSource(doubles = {1.5, 2.7, 3.14159})
void testWithDoubles(double value) {
assertTrue(value > 1.0);
}
@ParameterizedTest
@ValueSource(booleans = {true, false})
void testWithBooleans(boolean flag) {
// Test both true and false cases
if (flag) {
assertTrue(flag);
} else {
assertFalse(flag);
}
}
@ParameterizedTest
@ValueSource(classes = {String.class, Integer.class, List.class})
void testWithClasses(Class<?> clazz) {
assertNotNull(clazz);
assertNotNull(clazz.getName());
}
@ParameterizedTest
@ValueSource(chars = {'a', 'b', 'c'})
void testWithCharacters(char letter) {
assertTrue(Character.isLetter(letter));
assertTrue(Character.isLowerCase(letter));
}
}Provides a single null argument to parameterized tests.
/**
* Provides a single null argument to parameterized tests
* Cannot be used with primitive parameter types
*/
@Target({ElementType.ANNOTATION_TYPE, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@API(status = STABLE, since = "5.0")
@ArgumentsSource(NullArgumentsProvider.class)
@interface NullSource {
}Usage Examples:
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.NullSource;
import org.junit.jupiter.params.provider.ValueSource;
class NullSourceExamples {
@ParameterizedTest
@NullSource
void testWithNull(String value) {
assertNull(value);
}
// Combine with other sources
@ParameterizedTest
@NullSource
@ValueSource(strings = {"", " ", "apple"})
void testNullAndValues(String input) {
// Tests null, empty string, whitespace, and valid string
if (input == null) {
assertNull(input);
} else if (input.trim().isEmpty()) {
assertTrue(input.isEmpty() || input.trim().isEmpty());
} else {
assertFalse(input.trim().isEmpty());
}
}
@ParameterizedTest
@NullSource
void testNullCollection(List<String> list) {
assertNull(list);
}
}Provides empty values for supported types including strings, collections, maps, and arrays.
/**
* Provides empty values for supported types:
* - String: empty string ""
* - Collections: empty List, Set, SortedSet, NavigableSet
* - Maps: empty Map, SortedMap, NavigableMap
* - Arrays: empty primitive and object arrays
*/
@Target({ElementType.ANNOTATION_TYPE, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@API(status = STABLE, since = "5.0")
@ArgumentsSource(EmptyArgumentsProvider.class)
@interface EmptySource {
}Usage Examples:
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.EmptySource;
import org.junit.jupiter.params.provider.ValueSource;
import java.util.*;
class EmptySourceExamples {
@ParameterizedTest
@EmptySource
void testWithEmptyString(String value) {
assertEquals("", value);
assertTrue(value.isEmpty());
}
@ParameterizedTest
@EmptySource
void testWithEmptyList(List<String> list) {
assertNotNull(list);
assertTrue(list.isEmpty());
assertEquals(0, list.size());
}
@ParameterizedTest
@EmptySource
void testWithEmptyArray(String[] array) {
assertNotNull(array);
assertEquals(0, array.length);
}
@ParameterizedTest
@EmptySource
void testWithEmptyMap(Map<String, Integer> map) {
assertNotNull(map);
assertTrue(map.isEmpty());
assertEquals(0, map.size());
}
// Combine with other sources
@ParameterizedTest
@EmptySource
@ValueSource(strings = {"apple", "banana"})
void testEmptyAndValues(String input) {
assertNotNull(input);
if (input.isEmpty()) {
assertEquals("", input);
} else {
assertFalse(input.isEmpty());
}
}
}Composite annotation that combines @NullSource and @EmptySource functionality.
/**
* Composite annotation equivalent to combining @NullSource and @EmptySource
* Provides both null and empty values for the parameter type
*/
@Target({ElementType.ANNOTATION_TYPE, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@API(status = STABLE, since = "5.0")
@NullSource
@EmptySource
@interface NullAndEmptySource {
}Usage Examples:
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.NullAndEmptySource;
import org.junit.jupiter.params.provider.ValueSource;
class NullAndEmptySourceExamples {
@ParameterizedTest
@NullAndEmptySource
void testNullAndEmpty(String value) {
// Tests both null and empty string
assertTrue(value == null || value.isEmpty());
}
@ParameterizedTest
@NullAndEmptySource
void testNullAndEmptyList(List<String> list) {
// Tests both null and empty list
assertTrue(list == null || list.isEmpty());
}
// Combine with additional values
@ParameterizedTest
@NullAndEmptySource
@ValueSource(strings = {" ", "apple", "banana"})
void testNullEmptyAndValues(String input) {
// Tests null, empty, whitespace, and regular values
if (input == null) {
assertNull(input);
} else if (input.isEmpty()) {
assertEquals("", input);
} else if (input.trim().isEmpty()) {
assertFalse(input.isEmpty());
assertTrue(input.trim().isEmpty());
} else {
assertFalse(input.trim().isEmpty());
}
}
@ParameterizedTest
@NullAndEmptySource
@ValueSource(strings = {"valid"})
void testInputValidation(String input) {
boolean isValid = input != null && !input.trim().isEmpty();
if (input == null || input.isEmpty()) {
assertFalse(isValid);
} else {
assertTrue(isValid);
}
}
}Container annotations for multiple instances of repeatable value source annotations.
/**
* Container annotation for multiple @ValueSource annotations
*/
@Target({ElementType.ANNOTATION_TYPE, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@API(status = STABLE, since = "5.0")
@interface ValueSources {
ValueSource[] value();
}Usage Example:
class ContainerAnnotationExample {
// Multiple @ValueSource annotations automatically use container
@ParameterizedTest
@ValueSource(ints = {1, 2, 3})
@ValueSource(ints = {10, 20, 30})
void testMultipleValueSources(int value) {
assertTrue(value > 0);
// Tests: 1, 2, 3, 10, 20, 30
}
}These value-based sources provide the foundation for simple parameterized testing scenarios and are often combined with other argument sources for comprehensive test coverage.
Install with Tessl CLI
npx tessl i tessl/maven-org-junit-jupiter--junit-jupiter-params