0
0
JUnittesting~10 mins

@EnumSource for enum values in JUnit - Interactive Code Practice

Choose your learning style9 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.