0
0
JunitHow-ToBeginner ยท 4 min read

How to Use @EnumSource in JUnit 5 for Enum Parameterized Tests

Use @EnumSource in JUnit 5 to run a parameterized test with all or selected values of an enum. Annotate your test method with @ParameterizedTest and specify the enum class in @EnumSource to pass enum constants as test arguments automatically.
๐Ÿ“

Syntax

The @EnumSource annotation is used with @ParameterizedTest to supply enum constants as arguments to a test method.

  • @EnumSource(value = YourEnum.class): Runs the test with all enum constants.
  • names = {"CONSTANT1", "CONSTANT2"}: Runs the test only with specified enum constants.
  • mode = EnumSource.Mode.EXCLUDE: Excludes specified enum constants instead of including them.
java
@ParameterizedTest
@EnumSource(value = YourEnum.class, names = {"CONSTANT1", "CONSTANT2"}, mode = EnumSource.Mode.INCLUDE)
void testWithSelectedEnums(YourEnum enumValue) {
    // test code using enumValue
}
๐Ÿ’ป

Example

This example shows how to use @EnumSource to run a test method with all values of an enum Day. The test checks if the day is a weekend.

java
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.EnumSource;
import static org.junit.jupiter.api.Assertions.*;

public class EnumSourceExampleTest {

    enum Day {
        MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY
    }

    @ParameterizedTest
    @EnumSource(Day.class) // Runs test with all enum constants
    void testIsWeekend(Day day) {
        boolean isWeekend = (day == Day.SATURDAY || day == Day.SUNDAY);
        if (isWeekend) {
            assertTrue(isWeekend, day + " should be weekend");
        } else {
            assertFalse(isWeekend, day + " should not be weekend");
        }
    }
}
Output
Test run finished after 50 ms [ 7 containers found ] [ 0 containers skipped ] [ 7 containers started ] [ 0 containers aborted ] [ 7 containers successful ] [ 0 containers failed ] [ 14 tests found ] [ 0 tests skipped ] [ 14 tests started ] [ 0 tests aborted ] [ 14 tests successful ] [ 0 tests failed ]
โš ๏ธ

Common Pitfalls

Common mistakes when using @EnumSource include:

  • Not annotating the test method with @ParameterizedTest, which is required.
  • Using enum constants names incorrectly (case-sensitive and must match exactly).
  • Forgetting to import org.junit.jupiter.params.provider.EnumSource.
  • Trying to use @EnumSource with non-enum types.
java
/* Wrong: Missing @ParameterizedTest annotation */
@EnumSource(Day.class)
void testDay(Day day) {
    // This will not run as a parameterized test
}

/* Correct: Add @ParameterizedTest */
@ParameterizedTest
@EnumSource(Day.class)
void testDay(Day day) {
    // Runs with all enum values
}
๐Ÿ“Š

Quick Reference

Summary tips for using @EnumSource:

  • Use @ParameterizedTest with @EnumSource to run tests with enum values.
  • Specify value as the enum class.
  • Use names to include or exclude specific enum constants.
  • Enum constant names are case-sensitive and must match exactly.
  • Works only with enum types.
โœ…

Key Takeaways

Always use @ParameterizedTest together with @EnumSource to run enum-based parameterized tests.
Specify the enum class in @EnumSource to supply all or selected enum constants as test arguments.
Enum constant names in @EnumSource are case-sensitive and must match exactly.
Use the 'names' and 'mode' attributes to include or exclude specific enum constants.
@EnumSource works only with enum types and requires proper imports.