Challenge - 5 Problems
JUnit Multiple Parameter Types Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of JUnit test with multiple parameter types
Consider the following JUnit 5 parameterized test method using @CsvSource with mixed parameter types. What will be the test execution result for the given inputs?
JUnit
import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.CsvSource; import static org.junit.jupiter.api.Assertions.*; class CalculatorTest { @ParameterizedTest @CsvSource({ "2, 3, 5", "-1, 1, 0", "0, 0, 0", "5, -3, 2" }) void testAdd(int a, int b, int expected) { assertEquals(expected, a + b); } }
Attempts:
2 left
💡 Hint
Check the addition results for each input set carefully.
✗ Incorrect
Each test case adds the two integers and compares to the expected sum. All expected values match the actual sums, so all tests pass.
❓ assertion
intermediate1:30remaining
Correct assertion for mixed parameter types in JUnit
You have a parameterized test method with parameters: String name, int age, boolean active. Which assertion correctly verifies that age is greater than 18 and active is true?
JUnit
import static org.junit.jupiter.api.Assertions.*; void testUser(String name, int age, boolean active) { // Which assertion is correct? }
Attempts:
2 left
💡 Hint
Use assertTrue with the exact condition you want to verify.
✗ Incorrect
Option C directly asserts that age is greater than 18 and active is true. Option C works but is less clear. Option C asserts the negation, which is correct logically but less direct. Option C uses OR instead of AND, so it allows incorrect cases.
🔧 Debug
advanced2:30remaining
Identify the cause of failure in mixed parameter JUnit test
Given this parameterized test using @MethodSource, why does the test fail with a ParameterResolutionException?
JUnit
import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.MethodSource; import java.util.stream.Stream; class UserTest { static Stream<Object[]> userProvider() { return Stream.of( new Object[]{"Alice", 30, true}, new Object[]{"Bob", "twenty", false} ); } @ParameterizedTest @MethodSource("userProvider") void testUser(String name, int age, boolean active) { // test logic } }
Attempts:
2 left
💡 Hint
Check the types of values provided for each parameter.
✗ Incorrect
The second test case provides a String "twenty" where an int is expected, causing a ParameterResolutionException during test execution.
❓ framework
advanced1:30remaining
JUnit 5 parameterized test with multiple parameter types and custom converter
Which JUnit 5 feature allows you to convert a String parameter to a custom object type in a parameterized test?
Attempts:
2 left
💡 Hint
Look for annotations that handle parameter conversion explicitly.
✗ Incorrect
JUnit 5 provides @ConvertWith to specify a custom ArgumentConverter class to convert String inputs to custom types in parameterized tests.
🧠 Conceptual
expert3:00remaining
Handling multiple parameter types in JUnit parameterized tests with null values
In a JUnit 5 parameterized test with parameters (String name, Integer age, Boolean active), which approach correctly allows passing null values for age and active using @CsvSource?
Attempts:
2 left
💡 Hint
Consider how JUnit converts string literals to null for non-String parameters.
✗ Incorrect
JUnit 5 does not automatically convert the string 'null' to null for non-String parameters. Using a custom converter with @ConvertWith allows interpreting 'null' as null values.