Challenge - 5 Problems
JUnit ParameterizedTest Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of a simple @ParameterizedTest with @ValueSource
What will be the output when running this JUnit test class?
JUnit
import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.ValueSource; import static org.junit.jupiter.api.Assertions.assertTrue; public 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 @ParameterizedTest with @CsvSource
Given this parameterized test, which assertion correctly verifies that the sum of two integers equals the expected result?
JUnit
import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.CsvSource; import static org.junit.jupiter.api.Assertions.*; public class MathTest { @ParameterizedTest @CsvSource({"1, 2, 3", "4, 5, 9", "10, 20, 30"}) void testSum(int a, int b, int expected) { // Which assertion is correct here? } }
Attempts:
2 left
💡 Hint
Remember the order of parameters in assertEquals(expected, actual).
✗ Incorrect
The correct order for assertEquals is assertEquals(expected, actual). Option D uses this order correctly.
🔧 Debug
advanced2:00remaining
Identify the error in this @ParameterizedTest using @MethodSource
What error will occur when running this test class?
JUnit
import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.MethodSource; import static org.junit.jupiter.api.Assertions.assertTrue; import java.util.stream.Stream; public class DebugTest { @ParameterizedTest @MethodSource("stringProvider") void testStrings(String input) { assertTrue(input.length() > 0); } static Stream<String> stringProvider() { return Stream.of(""); } }
Attempts:
2 left
💡 Hint
Check the assertion condition and the provided input values.
✗ Incorrect
The input string is empty (length 0), so assertTrue(input.length() > 0) fails causing the test to fail.
🧠 Conceptual
advanced2:00remaining
Understanding @ParameterizedTest with multiple parameters
Which statement about @ParameterizedTest with @CsvSource is TRUE?
Attempts:
2 left
💡 Hint
Think about how multiple values are passed to the test method.
✗ Incorrect
Each line in @CsvSource provides a comma-separated list of values that map to the test method parameters in order.
❓ framework
expert2:00remaining
Behavior of @ParameterizedTest with @EnumSource and filtering
Consider this test using @EnumSource with a filter. Which enum constants will the test run with?
JUnit
import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.EnumSource; import static org.junit.jupiter.api.Assertions.assertNotNull; public class EnumTest { enum Color { RED, GREEN, BLUE, YELLOW } @ParameterizedTest @EnumSource(value = Color.class, names = {"RED", "BLUE"}, mode = EnumSource.Mode.EXCLUDE) void testColors(Color color) { assertNotNull(color); } }
Attempts:
2 left
💡 Hint
Check the meaning of mode = EXCLUDE with names specified.
✗ Incorrect
Using mode = EXCLUDE excludes RED and BLUE, so the test runs only with GREEN and YELLOW.