src/
└── test/
└── java/
└── com/
└── example/
├── enums/
│ └── Status.java
├── pages/
│ └── LoginPage.java
├── tests/
│ └── StatusEnumTest.java
└── utils/
└── TestUtils.java
@EnumSource for enum values in JUnit - Framework Patterns
- Enums Layer: Contains enum classes like
Statusrepresenting fixed sets of values. - Page Objects Layer: Encapsulates UI elements and actions (e.g.,
LoginPage). - Tests Layer: Contains test classes using JUnit, including parameterized tests with
@EnumSource. - Utilities Layer: Helper classes and methods for common test tasks.
- Configuration Layer: (Not shown in folder) Manages environment and test settings.
Use junit-platform.properties or system properties to configure test execution.
- Define environment variables for different test environments.
- Use Maven or Gradle profiles to select browsers or test parameters.
- Keep enum classes simple and stable to avoid breaking tests.
- JUnit generates XML reports compatible with CI tools like Jenkins or GitHub Actions.
- Use plugins to visualize test results and failures clearly.
- Integrate test runs in CI pipelines to run parameterized tests automatically.
- Reports show each enum value tested separately with pass/fail status.
- Use
@EnumSourceto run tests for all or selected enum values, ensuring full coverage. - Keep enum values stable to avoid test flakiness.
- Use descriptive test method names to clarify which enum value is tested.
- Combine
@EnumSourcewith other parameter sources for flexible tests. - Use explicit imports and avoid wildcard imports for clarity.
Where in this folder structure would you add a new enum class Priority to use with @EnumSource in tests?
Practice
What is the main purpose of using @EnumSource in JUnit tests?
Solution
Step 1: Understand the role of @EnumSource
@EnumSource is an annotation in JUnit that provides enum values as parameters to a test method.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.Final Answer:
To run the test method once for each value of an enum automatically -> Option DQuick Check:
@EnumSource runs tests per enum value = D [OK]
- Thinking @EnumSource creates enums
- Believing it skips enum values
- Confusing it with string conversion
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) { ... }Solution
Step 1: Recall correct @EnumSource syntax
The annotation requires the enum class name with .class, like Status.class.Step 2: Check each option
@EnumSource(Status.class) uses Status.class which is correct syntax. Others are invalid Java or annotation syntax.Final Answer:
@EnumSource(Status.class) -> Option AQuick Check:
Use enum class with .class in @EnumSource = A [OK]
- Omitting .class after enum name
- Using string instead of class reference
- Adding invalid parameters
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?
Solution
Step 1: Understand @EnumSource behavior
@EnumSource runs the test once for each enum constant in the order declared.Step 2: Identify output from System.out.println
Each test run prints the enum value. So output lines are RED, then GREEN, then BLUE.Final Answer:
RED GREEN BLUE -> Option CQuick Check:
Test runs once per enum value in order = C [OK]
- Assuming only some enum values run
- Thinking output order is random
- Expecting test failure without reason
Consider this test code snippet:
@ParameterizedTest
@EnumSource(Status.class)
void testStatus(String status) {
assertNotNull(status);
}What is the main problem here?
Solution
Step 1: Check parameter type vs enum type
@EnumSource(Status.class) passes enum constants of type Status, but method expects String parameter.Step 2: Understand type mismatch impact
This causes a runtime error because JUnit cannot convert Status enum to String automatically.Final Answer:
The test method parameter type does not match the enum type -> Option AQuick Check:
Parameter type must match enum type = A [OK]
- Using String instead of enum type as parameter
- Thinking @EnumSource only works with strings
- Assuming test method must be static
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?
Solution
Step 1: Recall how to filter enum values in @EnumSource
@EnumSource supports a 'names' attribute to specify which enum constants to include.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.Final Answer:
@EnumSource(value = TicketStatus.class, names = {"OPEN", "CLOSED"}) -> Option BQuick Check:
Use 'names' to select enum values in @EnumSource = B [OK]
- Using unsupported attributes like include or only
- Passing enum constants instead of strings in names
- Forgetting to specify value = Enum.class
Ready to prove it?
Pass the test and this pattern moves unknown
