Challenge - 5 Problems
Argument Conversion Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of JUnit parameterized test with argument conversion
Consider the following JUnit 5 parameterized test using a custom argument converter. What will be the output when the test runs?
JUnit
import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.converter.ArgumentConversionException; import org.junit.jupiter.params.converter.ArgumentConverter; import org.junit.jupiter.params.converter.ConvertWith; import static org.junit.jupiter.api.Assertions.*; class Temperature { private final double celsius; Temperature(double celsius) { this.celsius = celsius; } double getCelsius() { return celsius; } } class TemperatureConverter implements ArgumentConverter { @Override public Object convert(Object source, java.lang.reflect.Parameter context) throws ArgumentConversionException { if (source instanceof String s) { try { double f = Double.parseDouble(s); return new Temperature((f - 32) * 5 / 9); } catch (NumberFormatException e) { throw new ArgumentConversionException("Invalid temperature format"); } } throw new ArgumentConversionException("Unsupported source type"); } } public class TempTest { @ParameterizedTest @org.junit.jupiter.params.provider.ValueSource(strings = {"32", "212", "100"}) void testFreezingAndBoiling(@ConvertWith(TemperatureConverter.class) Temperature temp) { assertTrue(temp.getCelsius() >= 0 && temp.getCelsius() <= 100); } }
Attempts:
2 left
💡 Hint
Think about the conversion formula and the assertion range.
✗ Incorrect
The converter converts Fahrenheit strings to Celsius correctly. Inputs "32", "212", and "100" Fahrenheit convert to 0, 100, and 37.777... Celsius respectively, all within the asserted range 0 to 100, so all tests pass.
❓ assertion
intermediate1:30remaining
Correct assertion for converted argument in JUnit test
Given a JUnit parameterized test that converts a String to an Integer using a custom converter, which assertion correctly verifies the integer is positive?
JUnit
import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.converter.ConvertWith; import static org.junit.jupiter.api.Assertions.*; class PositiveIntegerConverter implements org.junit.jupiter.params.converter.ArgumentConverter { @Override public Object convert(Object source, java.lang.reflect.Parameter context) { return Integer.parseInt(source.toString()); } } public class NumberTest { @ParameterizedTest @org.junit.jupiter.params.provider.ValueSource(strings = {"5", "10", "-3"}) void testPositive(@ConvertWith(PositiveIntegerConverter.class) Integer number) { // Which assertion is correct here? } }
Attempts:
2 left
💡 Hint
Choose the assertion that directly checks positivity clearly and simply.
✗ Incorrect
assertTrue(number > 0); is the clearest and most direct assertion to verify the number is positive. Others are less clear or do not check positivity explicitly.
🔧 Debug
advanced2:00remaining
Identify the cause of ArgumentConversionException in JUnit test
This JUnit parameterized test throws ArgumentConversionException at runtime. What is the most likely cause?
JUnit
import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.converter.ArgumentConverter; import org.junit.jupiter.params.converter.ArgumentConversionException; import org.junit.jupiter.params.converter.ConvertWith; class BooleanStringConverter implements ArgumentConverter { @Override public Object convert(Object source, java.lang.reflect.Parameter context) throws ArgumentConversionException { if (source instanceof String s) { if (s.equalsIgnoreCase("true")) return Boolean.TRUE; if (s.equalsIgnoreCase("false")) return Boolean.FALSE; throw new ArgumentConversionException("Invalid boolean string"); } throw new ArgumentConversionException("Unsupported source type"); } } public class BoolTest { @ParameterizedTest @org.junit.jupiter.params.provider.ValueSource(strings = {"true", "false", "yes"}) void testBoolean(@ConvertWith(BooleanStringConverter.class) Boolean value) { assertNotNull(value); } }
Attempts:
2 left
💡 Hint
Check which input strings the converter accepts.
✗ Incorrect
The converter only accepts "true" or "false" strings (case-insensitive). The input "yes" is not recognized and causes ArgumentConversionException.
❓ framework
advanced1:30remaining
Best practice for argument conversion in JUnit 5 parameterized tests
Which practice is recommended when implementing a custom ArgumentConverter in JUnit 5?
Attempts:
2 left
💡 Hint
Think about how JUnit reports conversion errors.
✗ Incorrect
Throwing ArgumentConversionException with a clear message helps JUnit report conversion failures properly and helps debugging. Returning null or default values silently hides errors.
🧠 Conceptual
expert2:00remaining
Effect of argument conversion failure on JUnit 5 parameterized test execution
If a custom ArgumentConverter throws ArgumentConversionException for one parameter value in a JUnit 5 parameterized test, what happens to the test execution?
Attempts:
2 left
💡 Hint
Consider how JUnit handles parameterized test failures per argument.
✗ Incorrect
JUnit reports the test case with the failing argument conversion as failed or aborted but continues running other parameterized test cases with valid conversions.