Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using a non-enum type in @EnumSource.
Forgetting to add .class after the enum name.
✗ Incorrect
The @EnumSource annotation requires the enum class name. Here, 'Day' is the enum used for testing.
2fill in blank
mediumComplete 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using lowercase or misspelled enum names in 'names'.
Not enclosing enum names in quotes.
✗ Incorrect
The 'names' attribute specifies which enum constants to include. Here, MONDAY and FRIDAY are selected.
3fill in blank
hardFix 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Forgetting to set mode to EXCLUDE.
Listing wrong enum names to exclude.
✗ Incorrect
To exclude SUNDAY, it must be listed in the 'names' attribute with EXCLUDE mode.
4fill in blank
hardFill 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using partial enum names in 'names' attribute.
Mismatch between names and startsWith string.
✗ Incorrect
The test includes only TUESDAY, and the assertion checks the name starts with 'TUES'.
5fill in blank
hardFill 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong ordinal values for MONDAY or FRIDAY.
Mixing up enum names in the 'names' list.
✗ Incorrect
The test runs for MONDAY and FRIDAY. MONDAY ordinal is 1, FRIDAY ordinal is 5 (assuming enum starts at 0 with SUNDAY).