Challenge - 5 Problems
EnumSource Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of a JUnit test using @EnumSource with all enum values
What will be the output when running this JUnit 5 parameterized test using @EnumSource with all enum values?
JUnit
enum Color { RED, GREEN, BLUE }
@ParameterizedTest
@EnumSource(Color.class)
void testColors(Color color) {
System.out.println(color);
}Attempts:
2 left
💡 Hint
The @EnumSource without filters runs the test once for each enum constant.
✗ Incorrect
The @EnumSource(Color.class) runs the test method once for each enum constant in Color: RED, GREEN, and BLUE. Each call prints the enum name.
❓ assertion
intermediate2:00remaining
Correct assertion for filtered @EnumSource values
Given this enum and test, which assertion correctly verifies the test runs only for the filtered enum values?
JUnit
enum Status { SUCCESS, FAILURE, PENDING }
@ParameterizedTest
@EnumSource(value = Status.class, names = {"SUCCESS", "PENDING"})
void testStatus(Status status) {
// assertion here
}Attempts:
2 left
💡 Hint
The test runs only for SUCCESS and PENDING, so assertion must check these two.
✗ Incorrect
The @EnumSource filters to only SUCCESS and PENDING, so the status parameter will never be FAILURE. The assertion must check for SUCCESS or PENDING only.
🔧 Debug
advanced2:00remaining
Identify the error in @EnumSource usage
What error will occur when running this test code?
JUnit
enum Level { LOW, MEDIUM, HIGH }
@ParameterizedTest
@EnumSource(value = Level.class, names = {"LOW", "CRITICAL"})
void testLevel(Level level) {
System.out.println(level);
}Attempts:
2 left
💡 Hint
Check if all names in the names array exist in the enum.
✗ Incorrect
The enum Level does not have a constant named CRITICAL. Using a non-existent enum name in @EnumSource causes IllegalArgumentException at runtime.
🧠 Conceptual
advanced2:00remaining
Behavior of @EnumSource with mode EXCLUDE
What is the effect of using @EnumSource with mode = EnumSource.Mode.EXCLUDE and names = {"A", "B"} on an enum with constants A, B, C, D?
JUnit
enum Letter { A, B, C, D }
@ParameterizedTest
@EnumSource(value = Letter.class, mode = EnumSource.Mode.EXCLUDE, names = {"A", "B"})
void testLetter(Letter letter) {
System.out.println(letter);
}Attempts:
2 left
💡 Hint
Mode.EXCLUDE removes the named constants from the test parameters.
✗ Incorrect
Using mode = EXCLUDE with names {"A", "B"} excludes those constants from the test. The test runs only with C and D.
❓ framework
expert3:00remaining
Combining @EnumSource with custom argument conversion
Which code snippet correctly uses @EnumSource with a custom ArgumentConverter to convert enum values to lowercase strings for the test method parameter?
JUnit
enum Fruit { APPLE, BANANA, CHERRY }
public class LowercaseConverter implements ArgumentConverter {
@Override
public Object convert(Object source, ParameterContext context) {
return ((Fruit) source).name().toLowerCase();
}
}
@ParameterizedTest
@EnumSource(Fruit.class)
void testFruit(String fruitName) {
System.out.println(fruitName);
}Attempts:
2 left
💡 Hint
The @ConvertWith annotation must be on the parameter, not the method.
✗ Incorrect
To use a custom ArgumentConverter with @EnumSource, place @ConvertWith on the parameter to convert the enum to the desired type. Option A correctly applies @ConvertWith to the String parameter.