Bird
Raised Fist0
JUnittesting~10 mins

@EnumSource for enum values in JUnit - Interactive Code Practice

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to use @EnumSource to run the test for all enum values.

JUnit
@ParameterizedTest
@EnumSource([1].class)
void testEnumValues([1] value) {
    assertNotNull(value);
}
Drag options to blanks, or click blank then click option'
AString
Bint
CDay
DList
Attempts:
3 left
💡 Hint
Common Mistakes
Using a non-enum type in @EnumSource.
Forgetting to add .class after the enum name.
2fill in blank
medium

Complete the code to run the test only for the enum values MONDAY and FRIDAY.

JUnit
@ParameterizedTest
@EnumSource(value = Day.class, names = {"[1]", "FRIDAY"})
void testSelectedDays(Day day) {
    assertTrue(day == Day.MONDAY || day == Day.FRIDAY);
}
Drag options to blanks, or click blank then click option'
ASUNDAY
BMONDAY
CTUESDAY
DWEDNESDAY
Attempts:
3 left
💡 Hint
Common Mistakes
Using lowercase or misspelled enum names in 'names'.
Not enclosing enum names in quotes.
3fill in blank
hard

Fix the error in the code to exclude the enum value SUNDAY from the test.

JUnit
@ParameterizedTest
@EnumSource(value = Day.class, mode = EnumSource.Mode.EXCLUDE, names = {"[1]"})
void testExcludeSunday(Day day) {
    assertNotEquals(Day.SUNDAY, day);
}
Drag options to blanks, or click blank then click option'
AFRIDAY
BMONDAY
CSATURDAY
DSUNDAY
Attempts:
3 left
💡 Hint
Common Mistakes
Forgetting to set mode to EXCLUDE.
Listing wrong enum names to exclude.
4fill in blank
hard

Fill both blanks to run the test only for enum values starting with 'TUES'.

JUnit
@ParameterizedTest
@EnumSource(value = Day.class, names = {"[1]"}, mode = EnumSource.Mode.INCLUDE)
void testDaysStartingWithTues(Day day) {
    assertTrue(day.name().startsWith("[2]"));
}
Drag options to blanks, or click blank then click option'
ATUESDAY
BMONDAY
CTUES
DFRIDAY
Attempts:
3 left
💡 Hint
Common Mistakes
Using partial enum names in 'names' attribute.
Mismatch between names and startsWith string.
5fill in blank
hard

Fill all three blanks to run the test for enum values MONDAY and FRIDAY and assert their ordinal values.

JUnit
@ParameterizedTest
@EnumSource(value = Day.class, names = {"[1]", "[2]"})
void testDayOrdinals(Day day) {
    int ordinal = day.ordinal();
    assertTrue(ordinal == [3] || ordinal == 5);
}
Drag options to blanks, or click blank then click option'
AMONDAY
B0
C1
DFRIDAY
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong ordinal values for MONDAY or FRIDAY.
Mixing up enum names in the 'names' list.

Practice

(1/5)
1.

What is the main purpose of using @EnumSource in JUnit tests?

easy
A. To convert enum values to strings
B. To create a new enum type inside the test class
C. To skip tests for enum values
D. To run the test method once for each value of an enum automatically

Solution

  1. Step 1: Understand the role of @EnumSource

    @EnumSource is an annotation in JUnit that provides enum values as parameters to a test method.
  2. Step 2: Identify what it does in tests

    It runs the test method once for each enum constant, so all enum values are tested automatically.
  3. Final Answer:

    To run the test method once for each value of an enum automatically -> Option D
  4. Quick Check:

    @EnumSource runs tests per enum value = D [OK]
Hint: Remember: @EnumSource runs tests for every enum value [OK]
Common Mistakes:
  • Thinking @EnumSource creates enums
  • Believing it skips enum values
  • Confusing it with string conversion
2.

Which of the following is the correct syntax to use @EnumSource for an enum named Status in a JUnit 5 parameterized test?

@ParameterizedTest
@EnumSource(??? )
void testStatus(Status status) { ... }
easy
A. @EnumSource(Status.class)
B. @EnumSource("Status")
C. @EnumSource(Status)
D. @EnumSource(enum = Status)

Solution

  1. Step 1: Recall correct @EnumSource syntax

    The annotation requires the enum class name with .class, like Status.class.
  2. Step 2: Check each option

    @EnumSource(Status.class) uses Status.class which is correct syntax. Others are invalid Java or annotation syntax.
  3. Final Answer:

    @EnumSource(Status.class) -> Option A
  4. Quick Check:

    Use enum class with .class in @EnumSource = A [OK]
Hint: Use EnumName.class inside @EnumSource parentheses [OK]
Common Mistakes:
  • Omitting .class after enum name
  • Using string instead of class reference
  • Adding invalid parameters
3.

Given the enum Color { RED, GREEN, BLUE } and the test:

@ParameterizedTest
@EnumSource(Color.class)
void testColor(Color color) {
    System.out.println(color);
}

What will be the output when this test runs?

medium
A. BLUE GREEN RED
B. RED GREEN
C. RED GREEN BLUE
D. No output, test fails

Solution

  1. Step 1: Understand @EnumSource behavior

    @EnumSource runs the test once for each enum constant in the order declared.
  2. Step 2: Identify output from System.out.println

    Each test run prints the enum value. So output lines are RED, then GREEN, then BLUE.
  3. Final Answer:

    RED GREEN BLUE -> Option C
  4. Quick Check:

    Test runs once per enum value in order = C [OK]
Hint: Test runs once per enum value in declared order [OK]
Common Mistakes:
  • Assuming only some enum values run
  • Thinking output order is random
  • Expecting test failure without reason
4.

Consider this test code snippet:

@ParameterizedTest
@EnumSource(Status.class)
void testStatus(String status) {
    assertNotNull(status);
}

What is the main problem here?

medium
A. The test method parameter type does not match the enum type
B. @EnumSource cannot be used with Status enum
C. assertNotNull is not a valid assertion
D. The test method must be static

Solution

  1. Step 1: Check parameter type vs enum type

    @EnumSource(Status.class) passes enum constants of type Status, but method expects String parameter.
  2. Step 2: Understand type mismatch impact

    This causes a runtime error because JUnit cannot convert Status enum to String automatically.
  3. Final Answer:

    The test method parameter type does not match the enum type -> Option A
  4. Quick Check:

    Parameter type must match enum type = A [OK]
Hint: Method parameter type must be the enum type used in @EnumSource [OK]
Common Mistakes:
  • Using String instead of enum type as parameter
  • Thinking @EnumSource only works with strings
  • Assuming test method must be static
5.

You want to test only specific enum values OPEN and CLOSED from the enum TicketStatus { OPEN, IN_PROGRESS, CLOSED, REOPENED } using @EnumSource. Which is the correct way to do this?

hard
A. @EnumSource(TicketStatus.class, include = {OPEN, CLOSED})
B. @EnumSource(value = TicketStatus.class, names = {"OPEN", "CLOSED"})
C. @EnumSource(TicketStatus.class, only = {"OPEN", "CLOSED"})
D. @EnumSource(TicketStatus.class, filter = {OPEN, CLOSED})

Solution

  1. Step 1: Recall how to filter enum values in @EnumSource

    @EnumSource supports a 'names' attribute to specify which enum constants to include.
  2. Step 2: Check each option's syntax

    @EnumSource(value = TicketStatus.class, names = {"OPEN", "CLOSED"}) uses 'names' with string array correctly. Others use invalid or unsupported attributes.
  3. Final Answer:

    @EnumSource(value = TicketStatus.class, names = {"OPEN", "CLOSED"}) -> Option B
  4. Quick Check:

    Use 'names' to select enum values in @EnumSource = B [OK]
Hint: Use 'names' attribute with enum strings to filter values [OK]
Common Mistakes:
  • Using unsupported attributes like include or only
  • Passing enum constants instead of strings in names
  • Forgetting to specify value = Enum.class
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard