testcontainers/testcontainers-java · error · java.lang.IllegalArgumentException

Password cannot be null or empty

Error message

Password cannot be null or empty

What it means

OracleContainer.withPassword() validates that a password was supplied. A null or empty password would make the container unusable for JDBC connections, so the library throws IllegalArgumentException immediately at configuration time.

Solutions

  1. Pass a concrete non-empty password, e.g. withPassword("secret")
  2. Fix the credential source (env var, secret) so it returns a real value
  3. Omit withPassword and rely on the container's default password if a custom one isn't needed

Example fix

// before
container.withPassword(System.getenv("DB_PASS"));
// after
String pass = System.getenv("DB_PASS");
Assert.notNull(pass, "DB_PASS must be set");
container.withPassword(pass);
Defensive patterns

Strategy: validation

Validate before calling

if (password == null || password.isEmpty()) throw new IllegalStateException("DB password missing");
container.withPassword(password);

Type guard

boolean hasPassword(String p) { return p != null && !p.isEmpty(); }

Try / catch

try { container.withPassword(pass); } catch (IllegalArgumentException e) { throw new IllegalStateException("Configure DB_PASS before running tests", e); }

Prevention

When it happens

Trigger: Calling container.withPassword(null) or withPassword("") while building the OracleContainer.

Common situations: Password loaded from a missing env var/secret store; typos in config keys leaving the value empty; test fixtures that skip setting credentials.

Understand the failure class

Background: "must not be empty", "cannot be empty" — required-field validation errors across open-source libraries — this error's family across 41 libraries.

Related errors


AI-assisted analysis of testcontainers/testcontainers-java@8e549514e3 (2026-09-12). Data as JSON: /api/errors/8f7e6d66c5f9b578. Report an issue: GitHub.

Appendix: source

Thrown at modules/oracle-xe/src/main/java/org/testcontainers/containers/OracleContainer.java:163

        return usingSid;
    }

    @Override
    public OracleContainer withUsername(String username) {
        if (StringUtils.isEmpty(username)) {
            throw new IllegalArgumentException("Username cannot be null or empty");
        }
        if (ORACLE_SYSTEM_USERS.contains(username.toLowerCase())) {
            throw new IllegalArgumentException("Username cannot be one of " + ORACLE_SYSTEM_USERS);
        }
        this.username = username;
        return self();
    }

    @Override
    public OracleContainer withPassword(String password) {
        if (StringUtils.isEmpty(password)) {
            throw new IllegalArgumentException("Password cannot be null or empty");
        }
        this.password = password;
        return self();
    }

    @Override
    public OracleContainer withDatabaseName(String databaseName) {
        if (StringUtils.isEmpty(databaseName)) {
            throw new IllegalArgumentException("Database name cannot be null or empty");
        }

        if (DEFAULT_DATABASE_NAME.equals(databaseName.toLowerCase())) {
            throw new IllegalArgumentException("Database name cannot be set to " + DEFAULT_DATABASE_NAME);
        }

        this.databaseName = databaseName;
        return self();
    }

View on GitHub (pinned to 8e549514e3)