Challenge - 5 Problems
JUnit @ValueSource Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of a parameterized test with @ValueSource
What will be the output when running this JUnit 5 parameterized test?
JUnit
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); } }
Attempts:
2 left
💡 Hint
Check the length of each string in the @ValueSource array.
✗ Incorrect
Each string in the array has length greater than 3, so the assertion assertTrue(fruit.length() > 3) passes for all.
❓ assertion
intermediate2:00remaining
Correct assertion for integer values from @ValueSource
Given this parameterized test, which assertion correctly verifies that the input number is even?
JUnit
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? } }
Attempts:
2 left
💡 Hint
Even numbers have zero remainder when divided by 2.
✗ Incorrect
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.
🔧 Debug
advanced2:00remaining
Identify the error in this @ValueSource usage
What error will occur when running this test class?
JUnit
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); } }
Attempts:
2 left
💡 Hint
Check if @ValueSource allows null elements in its arrays.
✗ Incorrect
@ValueSource does not support null values and throws IllegalArgumentException when null is included.
🧠 Conceptual
advanced2:00remaining
Behavior of @ValueSource with empty arrays
What happens when a parameterized test uses @ValueSource with an empty array?
JUnit
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); } }
Attempts:
2 left
💡 Hint
Think about how parameterized tests behave with no parameters.
✗ Incorrect
When @ValueSource provides an empty array, the test method is not executed at all, so it runs zero times and passes silently.
❓ framework
expert2:00remaining
Combining @ValueSource with other parameter sources
Which statement about combining @ValueSource with other parameter sources in JUnit 5 is correct?
Attempts:
2 left
💡 Hint
Check JUnit 5 rules about parameter sources per test method.
✗ Incorrect
JUnit 5 allows only one parameter source annotation per @ParameterizedTest method. Combining @ValueSource with others like @CsvSource in the same method is not allowed.