#1 India's Top IT Training Institute
×

About Uncodemy

Know more about who we are and what we stand for.

About Us

About Us

Discover our mission to equip individuals with essential IT skills and industry expertise.

Read More →
UnCodeMy Gallery

UnCodeMy Gallery

Explore highlights from our IT training sessions and success stories.

Read More →
Mentors

Mentors

Meet the industry experts who train and guide you through your course.

Read More →
Free Resources

Free Resources

Free tutorials, interview questions, tools and calculators to help you learn.

Explore →
×

Free Resources

Learn, practice and plan your career — bilkul free.

Tutorials

Tutorials

Step-by-step written tutorials to learn any technology at your own pace.

Read More →
Free Counselling

Free Counselling

Book a free video counselling session and pick the right course for you.

Read More →
Online Compiler

Online Compiler

Write, run and test your code online — no setup required.

Read More →
Core Java

Java Enums

What is an Enum?

An enum is a special Java type that represents a fixed set of named constants, making code more readable and type-safe than using plain strings or numbers.

enum Status { PENDING, APPROVED, REJECTED }
Status s = Status.APPROVED;

Why Testers Use Enums

Enums are useful for representing a fixed set of values like HTTP methods or environment names, preventing typos that plain strings would allow (e.g., "aproved" instead of "APPROVED").

Enums With Fields, Constructors and Methods

A Java enum is a full class, so each constant can carry data. Test frameworks use this to keep environment details in one typed place instead of scattered strings.

public enum Environment {
    DEV("https://dev.example.com"),
    QA("https://qa.example.com"),
    STAGING("https://staging.example.com");

    private final String baseUrl;

    Environment(String baseUrl) {     // implicitly private
        this.baseUrl = baseUrl;
    }

    public String getBaseUrl() {
        return baseUrl;
    }
}

// Pick the environment from a system property, default QA
String name = System.getProperty("env", "QA");
Environment env = Environment.valueOf(name.toUpperCase());
driver.get(env.getBaseUrl() + "/login");

Running the suite with mvn test -Denv=staging now points every test at staging without touching code. A misspelled value fails fast instead of quietly hitting the wrong server.

Built-in Methods You Get for Free

MethodWhat it returns
values()An array of all constants in declaration order
valueOf(String)The constant with that exact name, or an IllegalArgumentException
name()The constant's name as written in code
ordinal()Its position, starting from 0

Enum constants are singletons, so comparing them with == is safe and also null-safe, unlike calling equals on a possibly null variable.

Enums also work neatly as test parameters. In JUnit 5, @EnumSource(Environment.class) on a parameterized test runs the same method once for every constant, and TestNG data providers can return Environment.values() for the same effect. Adding a constant then automatically adds a test run, with no list of strings to keep in sync.

A Browser Factory Driven by an Enum

Switching on an enum gives you compile-time checking of every option. When someone adds a new browser constant, you have one obvious place to update.

public enum Browser { CHROME, FIREFOX, EDGE }

public static WebDriver createDriver(Browser browser) {
    switch (browser) {
        case CHROME:
            return new ChromeDriver();
        case FIREFOX:
            return new FirefoxDriver();
        case EDGE:
            return new EdgeDriver();
        default:
            throw new IllegalArgumentException("Unsupported browser: " + browser);
    }
}

// Run a check across every browser
for (Browser b : Browser.values()) {
    WebDriver driver = createDriver(b);
    // ... test steps ...
    driver.quit();
}

Other handy testing uses include an enum for user roles in permission tests, an enum for expected order statuses, and EnumMap to hold expected results keyed by those statuses.

Pitfalls With Enums in Test Code

  • Case-sensitive lookup. valueOf("qa") throws an exception because the constant is QA. Normalise input with toUpperCase() first.
  • Depending on ordinal(). Inserting a new constant in the middle shifts every position, breaking anything that stored the number.
  • Using name() for display text. UI labels like "In Progress" rarely match a constant name. Add a field for the display value and assert against that.
  • Trying to extend an enum. Enums already extend java.lang.Enum, so they cannot extend another class, though they can implement interfaces.
  • Hiding test data in enums. Usernames and passwords that change often belong in configuration files or a secrets store, not compiled code.

Rule of thumb: if a value comes from a small, fixed list that your tests check against, make it an enum; if it changes per run or per environment, keep it in configuration.

Ready to master real-world software testing?

Learn manual and automation testing hands-on with mentor-led sessions.

Explore Course