0
0
JUnittesting~5 mins

@EnumSource for enum values in JUnit - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
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?
ARuns the test with all enum values except the first
BRuns the test once for each value in MyEnum
CRuns the test only once with the first enum value
DRuns the test with no enum values
How do you run a test with only specific enum values using @EnumSource?
AUse the <code>names</code> attribute with the enum constants you want
BUse <code>excludeNames</code> attribute
CUse <code>value</code> attribute with a list of values
DYou cannot filter enum values with <code>@EnumSource</code>
If the test method parameter type is an enum, can you omit the enum class in @EnumSource?
AOnly if the enum is public
BNo, you must always specify the enum class
CYes, JUnit infers the enum class from the parameter type
DOnly if the enum has less than 5 values
What type of test is @EnumSource mainly used with?
AUnit tests without parameters
BPerformance tests
CIntegration tests
DParameterized tests
Which of these is a valid way to use @EnumSource?
A@EnumSource(value = Status.class, names = {"ACTIVE", "INACTIVE"})
B@EnumSource(Status.ACTIVE, Status.INACTIVE)
C@EnumSource(Status)
D@EnumSource(names = Status.class)
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.