testcontainers/testcontainers-java · error · IllegalArgumentException
Null password is not allowed
Error message
Null password is not allowed
What it means
MSSQLServerContainer.withPassword() validates the supplied sa password via checkPasswordStrength(). If the argument is null, it throws this IllegalArgumentException immediately rather than deferring failure to container startup. SQL Server images reject empty/null credentials, so the library fails fast.
Solutions
- Pass a non-null password string, e.g. withPassword("yourStrong(!)Password")
- Check the config source that produced null and set the password before calling withPassword()
- Use a container default like MSSQLServerContainer.DEFAULT_PASSWORD if you did not intend to override it
Example fix
// before
container.withPassword(System.getenv("SA_PASSWORD")); // may be null
// after
String pw = System.getenv("SA_PASSWORD");
Objects.requireNonNull(pw, "SA_PASSWORD must be set");
container.withPassword(pw); Defensive patterns
Strategy: validation
Validate before calling
if (password == null) { throw new IllegalStateException("SA password must be set before withPassword()"); } Type guard
boolean hasPassword = password != null && !password.isBlank();
Try / catch
try { container.withPassword(pw); } catch (IllegalArgumentException e) { throw new IllegalStateException("Invalid SA password config: " + e.getMessage(), e); } Prevention
- Read passwords with getOrDefault or requireNonNull to surface missing keys early
- Never pass environment/config lookups directly into withPassword() without a null check
When it happens
Trigger: Calling withPassword(null) on modules/mssqlserver MSSQLServerContainer, or passing a variable that was never initialized / loaded from a null config value.
Common situations: Password read from a properties/env loader that returns null when the key is absent; test fixtures where the password field is left unset; refactors that renamed the config key.
Related errors
- Null password is not allowed
- Password can be up to 128 characters long
- Password can be up to 128 characters long
- Password must contain characters from three of the…
- Password must contain characters from three of the…
AI-assisted analysis of testcontainers/testcontainers-java@8e549514e3 (2026-09-12).
Data as JSON: /api/errors/0cdf5d7685f4925c.
Report an issue: GitHub.
Appendix: source
Thrown at modules/mssqlserver/src/main/java/org/testcontainers/containers/MSSQLServerContainer.java:142
public String getPassword() {
return password;
}
@Override
public String getTestQueryString() {
return "SELECT 1";
}
@Override
public SELF withPassword(final String password) {
checkPasswordStrength(password);
this.password = password;
return self();
}
private void checkPasswordStrength(String password) {
if (password == null) {
throw new IllegalArgumentException("Null password is not allowed");
}
if (password.length() < 8) {
throw new IllegalArgumentException("Password should be at least 8 characters long");
}
if (password.length() > 128) {
throw new IllegalArgumentException("Password can be up to 128 characters long");
}
long satisfiedCategories = Stream
.of(PASSWORD_CATEGORY_VALIDATION_PATTERNS)
.filter(p -> p.matcher(password).find())
.count();
if (satisfiedCategories < 3) {
throw new IllegalArgumentException(
"Password must contain characters from three of the following four categories:\n" +View on GitHub (pinned to 8e549514e3)