testcontainers/testcontainers-java · error · IllegalStateException
Cannot set Elasticsearch URL when using Elasticsearch…
Error message
Cannot set Elasticsearch URL when using Elasticsearch container
What it means
KibanaContainer supports two modes: managed (it launches its own Elasticsearch container) and external (it points at an Elasticsearch URL you supply). withElasticsearchUrl(String) throws IllegalStateException if an Elasticsearch container was already configured via withElasticsearch(...), because a Kibana container cannot target both.
Solutions
- Remove the withElasticsearch(elasticsearchContainer) call if you want external mode.
- Or remove the withElasticsearchUrl(...) call if you want managed mode.
- Keep exactly one mode configuration per KibanaContainer instance.
Example fix
// before
new KibanaContainer(IMAGE)
.withElasticsearch(esContainer)
.withElasticsearchUrl("http://es.local:9200"); // throws
// after
new KibanaContainer(IMAGE)
.withElasticsearchUrl("http://es.local:9200"); Defensive patterns
Strategy: validation
Validate before calling
assert !(usingManagedEs && externalEsUrl != null) : "KibanaContainer supports either managed or external Elasticsearch, not both";
Prevention
- Choose managed vs external mode once, at builder construction.
- Delete withElasticsearch(...) when migrating to withElasticsearchUrl(...).
- Wrap container creation in a single factory method to enforce the invariant.
- Review test base classes for leftover mode configuration.
When it happens
Trigger: Calling withElasticsearchUrl(...) on a KibanaContainer that was constructed/configured with withElasticsearch(elasticsearchContainer) (or vice versa in the opposite order).
Common situations: Refactoring tests from managed to external mode and leaving the withElasticsearch(...) call in place; copying configuration from another test class.
Understand the failure class
Background: Conflicting config options: "cannot be used together" — configuration validation errors across open-source libraries — this error's family across 162 libraries.
Related errors
- withReuse(true) is not supported for KibanaContainer in…
- Conflicting Elasticsearch credentials: provide either a…
- Elasticsearch containerId is not available. In managed…
- Failed to connect Elasticsearch container to ad-hoc shared…
- Cannot create service account token in external mode
AI-assisted analysis of testcontainers/testcontainers-java@8e549514e3 (2026-09-12).
Data as JSON: /api/errors/d8dc4908bca75a5b.
Report an issue: GitHub.
Appendix: source
Thrown at modules/elasticsearch/src/main/java/org/testcontainers/elasticsearch/KibanaContainer.java:173
if (reusable && elasticsearch != null) {
throw new IllegalStateException(
"withReuse(true) is not supported for KibanaContainer in managed mode. " +
"Use external mode (withElasticsearchUrl) to enable reuse."
);
}
return super.withReuse(reusable);
}
/**
* Configures the Elasticsearch URL for external mode.
*
* @param elasticsearchUrl the Elasticsearch URL (e.g., "https://my.fancy.setup.elastic.cloud:9200")
* @return this container instance
* @throws IllegalStateException if already using managed mode
*/
public KibanaContainer withElasticsearchUrl(String elasticsearchUrl) {
if (elasticsearch != null) {
throw new IllegalStateException("Cannot set Elasticsearch URL when using Elasticsearch container");
}
this.elasticsearchUrl = elasticsearchUrl;
return this;
}
/**
* Configures Kibana to authenticate using the kibana_system user.
*
* @param password the password for the kibana_system user
* @return this container instance
*/
public KibanaContainer withKibanaSystemPassword(String password) {
return withKibanaUsernameAndPassword(KIBANA_SYSTEM_USER, password);
}
/**
* Configures credentials Kibana will use for authentication.
*View on GitHub (pinned to 8e549514e3)