Recall & Review
beginner
What is the purpose of the
@EnumSource annotation in JUnit?@EnumSource is used to run a parameterized test with all or some values of an enum. It helps test methods with enum inputs easily.
Click to reveal answer
beginner
How do you specify which enum to use with
@EnumSource?<p>You provide the enum class as a parameter, like <code>@EnumSource(MyEnum.class)</code>. This tells JUnit to use all values of <code>MyEnum</code> for the test.</p>Click to reveal answer
intermediate
How can you run a test with only specific enum values using
@EnumSource?Use the names attribute to list enum constants you want, e.g., @EnumSource(value = MyEnum.class, names = {"VALUE1", "VALUE2"}).
Click to reveal answer
intermediate
What happens if you use
@EnumSource without specifying an enum class?<p>JUnit expects the parameter type of the test method to be an enum. It uses that enum class automatically.</p>Click to reveal answer
beginner
Write a simple example of a JUnit test method using
@EnumSource to test all values of an enum Day.enum Day { MONDAY, TUESDAY, WEDNESDAY }
@ParameterizedTest
@EnumSource(Day.class)
void testDayEnum(Day day) {
assertNotNull(day);
}Click to reveal answer
What does
@EnumSource(MyEnum.class) do in a JUnit test?✗ Incorrect
@EnumSource(MyEnum.class) runs the test once for each enum constant in MyEnum.
How do you run a test with only specific enum values using
@EnumSource?✗ Incorrect
The names attribute lets you specify which enum constants to include.
If the test method parameter type is an enum, can you omit the enum class in
@EnumSource?✗ Incorrect
JUnit can infer the enum class from the test method parameter type if you omit it.
What type of test is
@EnumSource mainly used with?✗ Incorrect
@EnumSource is designed for parameterized tests to supply enum values.
Which of these is a valid way to use
@EnumSource?✗ Incorrect
Option A correctly uses value and names attributes.
Explain how
@EnumSource works in JUnit parameterized tests and how you can select specific enum values.Think about how JUnit uses enum values to run tests multiple times.
You got /4 concepts.
Describe a simple example of a JUnit test method using
@EnumSource with an enum parameter.Imagine testing all days of the week using an enum.
You got /4 concepts.