Argument conversion is the process where JUnit automatically changes the test method's input parameters from strings (like those in @ParameterizedTest) into the required data types before running the test.
Click to reveal answer
intermediate
How does JUnit know how to convert a String argument to a custom object?
JUnit looks for a constructor or a static method in the custom class that takes a single String argument to convert the input string into the object.
Click to reveal answer
intermediate
Which annotation in JUnit 5 allows you to provide a custom argument converter?
You use the @ConvertWith annotation to specify a custom ArgumentConverter class that tells JUnit how to convert the input argument.
Click to reveal answer
beginner
True or False: JUnit can only convert arguments to primitive types automatically.
False. JUnit can convert to many types automatically, including primitives, wrappers, enums, and even custom types if they have the right constructor or static method.
Click to reveal answer
beginner
What happens if JUnit cannot convert a parameter to the required type during a parameterized test?
The test will fail with an error indicating that the argument conversion failed, usually showing a message about no suitable constructor or method found.
Click to reveal answer
In JUnit, which of these is NOT a way JUnit converts a String argument to a parameter type?
AUsing a static method named valueOf or fromString
BUsing the toString() method of the parameter type
CUsing a method annotated with @ConvertWith
DUsing a constructor that takes a String
✗ Incorrect
JUnit does not use the toString() method for argument conversion. It uses constructors, static methods like valueOf, or custom converters with @ConvertWith.
What annotation do you use to specify a custom argument converter in JUnit?
A@ConvertArgument
B@ArgumentConverter
C@ConvertWith
D@CustomConverter
✗ Incorrect
The correct annotation to specify a custom argument converter is @ConvertWith.
If you have a parameterized test method with a parameter of type MyClass, how can JUnit convert a String to MyClass automatically?
AEither A or B
BMyClass must have a static method named valueOf(String)
CMyClass must have a constructor that takes a String
The string "42" is converted to Integer 42 by MyConverter using Integer.parseInt.
Step 2: Calculate the printed output
The test method adds 8 to the converted Integer 42, resulting in 50 printed.
Final Answer:
50 -> Option A
Quick Check:
42 + 8 = 50 [OK]
Hint: Convert string to int, then add 8 for output [OK]
Common Mistakes:
Assuming no conversion happens
Expecting string concatenation instead of addition
Thinking code causes errors
4. Identify the error in this custom converter code:
public class MyConverter extends SimpleArgumentConverter {
@Override
protected Object convert(Object source, Class targetType) {
if (targetType != Integer.class) {
throw new IllegalArgumentException("Conversion only supports Integer");
}
return source;
}
}
medium
A. The method should return Integer, not Object
B. The source is returned without converting to Integer
C. The targetType check should use equals() method
D. The class should implement ArgumentConverter interface
Solution
Step 1: Analyze the convert method's return value
The method returns source directly, which is a String, not converted to Integer.
Step 2: Identify the missing conversion
It should parse the source string to Integer before returning, e.g., Integer.parseInt(source.toString()).
Final Answer:
The source is returned without converting to Integer -> Option B
Quick Check:
Return value must be converted type [OK]
Hint: Always convert source to target type before returning [OK]
Common Mistakes:
Returning source without conversion
Using != instead of equals() for class check (less critical)
Confusing return type requirements
5. You want to write a parameterized test that accepts a CSV string like "John,25,true" and converts it into a custom Person object with fields name (String), age (int), and active (boolean). Which approach correctly uses argument conversion in JUnit?
hard
A. Use @ValueSource(strings = {"John,25,true"}) and parse CSV inside the test method manually
B. Use @CsvSource with three separate parameters and no converter
C. Create a converter implementing ArgumentProvider and use @ArgumentsSource annotation
D. Create a converter extending SimpleArgumentConverter that splits the CSV and constructs Person; annotate parameter with @ConvertWith using this converter
Solution
Step 1: Understand the need for custom conversion
The input is a single CSV string that must be converted into a Person object before the test runs.
Step 2: Identify the correct use of @ConvertWith
Creating a converter extending SimpleArgumentConverter that parses the CSV and returns a Person object, then annotating the test parameter with @ConvertWith, is the proper approach.
Final Answer:
Create a converter extending SimpleArgumentConverter that splits the CSV and constructs Person; annotate parameter with @ConvertWith using this converter -> Option D