Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

In JUnit Jupiter 5.7.0, @EnumSource supplies enum constants to a parameterized test, running the test once for each selected constant. Use it when enum values are the test cases: leave the names unset to exercise every constant, or combine names with a selection mode to include, exclude, or pattern-match constants.

What @EnumSource does in JUnit 5.7

@EnumSource is an argument source for @ParameterizedTest. It draws its arguments from a Java enum, so each selected constant becomes an input to the test method. The JUnit 5.7.0 User Guide demonstrates this with ChronoUnit constants passed to a test whose parameter is declared as the broader TemporalUnit interface. See the JUnit 5.7.0 @EnumSource documentation.

Set up the parameterized-test dependency

The JUnit 5.7.0 artifact overview identifies junit-jupiter-params as the module that supports parameterized tests. Ensure that this module is available and that its version aligns with the JUnit version used by the project; the guide does not establish compatibility details for every build tool or execution environment. See the JUnit 5.7.0 dependency metadata.

Choose how JUnit identifies the enum

Infer the type from an enum parameter

If the test method’s first parameter is itself an enum type, you can omit the annotation’s enum class. For example, the declared parameter below is ChronoUnit, so JUnit can infer the source type:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.EnumSource;

import static org.junit.jupiter.api.Assertions.assertNotNull;

class ChronoUnitTest {
    @ParameterizedTest
    @EnumSource
    void acceptsEveryUnit(ChronoUnit unit) {
        assertNotNull(unit);
    }
}

This inference uses the declared parameter type, not the type of a value at runtime. The parameter must be the enum itself for this form to work.

Name the enum explicitly for an interface parameter

If the test parameter is an interface implemented by an enum, specify the enum class in @EnumSource. For instance, TemporalUnit is an interface, so it does not tell JUnit which enum should provide the arguments:

import java.time.temporal.TemporalUnit;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.EnumSource;

import static org.junit.jupiter.api.Assertions.assertNotNull;

class TemporalUnitTest {
    @ParameterizedTest
    @EnumSource(ChronoUnit.class)
    void acceptsChronoUnits(TemporalUnit unit) {
        assertNotNull(unit);
    }
}

The explicit source type can also make the intended enum clearer when the method parameter is broader than that enum. The JUnit 5.7.0 guide documents the inference and explicit-type forms in its @EnumSource section.

Select constants by name or pattern

With no names specified, @EnumSource supplies all constants. When a test needs only a subset, list constant names and choose the selection mode that matches the intended cases.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Include specific constants

By default, naming constants selects those constants. This test receives only DAYS and HOURS:

import java.time.temporal.ChronoUnit;
import java.util.EnumSet;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.EnumSource;

import static org.junit.jupiter.api.Assertions.assertTrue;

class SelectedUnitsTest {
    @ParameterizedTest
    @EnumSource(names = { "DAYS", "HOURS" })
    void receivesSelectedUnits(ChronoUnit unit) {
        assertTrue(EnumSet.of(ChronoUnit.DAYS, ChronoUnit.HOURS).contains(unit));
    }
}

Exclude specific constants

Use mode = EXCLUDE to omit the named constants while supplying the other enum constants. The assertion should verify that the omitted values are absent:

Rank #4
Sale
import java.time.temporal.ChronoUnit;
import java.util.EnumSet;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.EnumSource;

import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.params.provider.EnumSource.Mode.EXCLUDE;

class ExcludedUnitsTest {
    @ParameterizedTest
    @EnumSource(mode = EXCLUDE, names = { "ERAS", "FOREVER" })
    void doesNotReceiveExcludedUnits(ChronoUnit unit) {
        assertFalse(EnumSet.of(ChronoUnit.ERAS, ChronoUnit.FOREVER).contains(unit));
    }
}

Match names with a regular expression

With mode = MATCH_ALL, the supplied regular expression is matched against enum constant names. This example selects constants whose names end in DAYS and checks that same condition:

import java.time.temporal.ChronoUnit;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.EnumSource;

import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.params.provider.EnumSource.Mode.MATCH_ALL;

class DayUnitsTest {
    @ParameterizedTest
    @EnumSource(mode = MATCH_ALL, names = "^.*DAYS$")
    void receivesNamesEndingInDays(ChronoUnit unit) {
        assertTrue(unit.name().endsWith("DAYS"));
    }
}

The JUnit 5.7.0 guide documents the available selection patterns and examples in its @EnumSource reference.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Best Value
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

When to use @MethodSource instead

Use @EnumSource when the test inputs are naturally enum constants. If cases must be produced by a factory method—for example, because the inputs are structured arguments rather than individual values from one enum—JUnit 5.7.0 documents @MethodSource as an alternative that supplies arguments from factory methods and streams. The distinction is about where the cases come from: enum constants versus factory-produced arguments. See the JUnit 5.7.0 @MethodSource documentation.

Common mistakes to avoid

  • Leaving the enum type implicit for an interface parameter: name the enum class explicitly, such as @EnumSource(ChronoUnit.class) for a TemporalUnit parameter.
  • Assuming names is mandatory: without it, all enum constants are supplied.
  • Using an assertion that contradicts the selection: for an exclusion source, verify that the excluded constants are not received; for a regex source, check the matching name condition.
  • Using an enum source for non-enum cases: choose a factory-backed source such as @MethodSource when the test cases are better generated as arguments.

This explanation is specific to the official JUnit 5.7.0 User Guide, last updated 2020-08-14; it does not establish the behavior of later JUnit versions. Consult the documentation and dependency configuration for the version your project actually uses.

Quick Recap

SaleBestseller No. 3
SaleBestseller No. 4
Pragmatic Unit Testing in Java with JUnit
Pragmatic Unit Testing in Java with JUnit
Used Book in Good Condition
$13.88
SaleBestseller No. 5

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.