import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.ValueSource; import static org.junit.jupiter.api.Assertions.assertTrue; class SampleTest { @ParameterizedTest @ValueSource(strings = {"apple", "banana", "cherry"}) void testStringLength(String fruit) { assertTrue(fruit.length() > 3); } }
Each string in the array has length greater than 3, so the assertion assertTrue(fruit.length() > 3) passes for all.
import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.ValueSource; import static org.junit.jupiter.api.Assertions.*; class NumberTest { @ParameterizedTest @ValueSource(ints = {2, 4, 6, 7}) void testIsEven(int number) { // Which assertion is correct here? } }
The assertion assertTrue(number % 2 == 0) correctly checks if the number is even.
Option B is the opposite, C checks for odd numbers, and D compares division result incorrectly.
import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.ValueSource; class DebugTest { @ParameterizedTest @ValueSource(strings = {"one", null, "three"}) void testStrings(String input) { System.out.println(input); } }
@ValueSource does not support null values and throws IllegalArgumentException when null is included.
import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.ValueSource; class EmptyTest { @ParameterizedTest @ValueSource(strings = {}) void testEmpty(String input) { System.out.println(input); } }
When @ValueSource provides an empty array, the test method is not executed at all, so it runs zero times and passes silently.
JUnit 5 allows only one parameter source annotation per @ParameterizedTest method. Combining @ValueSource with others like @CsvSource in the same method is not allowed.
