testcontainers/testcontainers-java · error · IllegalStateException

withReuse(true) is not supported for KibanaContainer in…

Error message

withReuse(true) is not supported for KibanaContainer in managed mode. Use external mode (withElasticsearchUrl) to enable reuse.

What it means

KibanaContainer overrides withReuse(boolean) and refuses reuse when the container is in managed mode (an Elasticsearch container was supplied via withElasticsearch(...)), because a reusable Kibana instance depends on a managed ES container whose lifecycle cannot be shared across test runs. Reuse is only supported in external mode where Kibana points at a fixed Elasticsearch URL.

Solutions

  1. Switch to external mode: call withElasticsearchUrl("http://host:9200") pointing at an already-running/reusable Elasticsearch instead of withElasticsearch(...).
  2. Then call withReuse(true) — it will pass through to the parent container implementation.
  3. Enable Testcontainers reuse support (.testcontainers.properties: testcontainers.reuse.enable=true) and ensure credentials/config are stable across runs.
  4. Alternatively keep managed mode and drop withReuse(true).

Example fix

// before
new KibanaContainer(IMAGE)
    .withElasticsearch(esContainer)
    .withReuse(true); // throws
// after
new KibanaContainer(IMAGE)
    .withElasticsearchUrl("http://localhost:9200")
    .withReuse(true);
Defensive patterns

Strategy: validation

Validate before calling

boolean managedMode = esContainer != null; boolean reuseDesired = true; if (managedMode && reuseDesired) { /* switch to withElasticsearchUrl instead */ }

Prevention

When it happens

Trigger: Calling kibana.withReuse(true) after (or before) using withElasticsearch(elasticsearchContainer) — i.e. any reuse attempt in managed mode.

Common situations: Developers enabling Testcontainers reuse to speed up slow Kibana startups while also letting the library spin up its own Elasticsearch container.

Understand the failure class

Background: UnsupportedOperationException and "is not supported" errors: when a library deliberately refuses a call — this error's family across 30 libraries.

Related errors


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

Appendix: source

Thrown at modules/elasticsearch/src/main/java/org/testcontainers/elasticsearch/KibanaContainer.java:156

     * <p><b>Supported in external mode only.</b> When Kibana is configured via
     * {@link #withElasticsearchUrl(String)}, the container configuration is fully deterministic
     * and TC can reliably locate the running container on subsequent runs.
     *
     * <p>Reuse is <b>not supported in managed mode</b> (i.e. when this container was created with
     * an {@link ElasticsearchContainer}). Managed mode introduces several non-deterministic inputs
     * into the container hash on every run (ad-hoc network ID, random network alias, fresh service
     * account token), so TC always sees a different hash and starts a fresh container instead of
     * reusing the existing one. This is a framework-level characteristic that affects any container
     * connected via {@code withNetwork()} — not specific to {@code KibanaContainer}.
     *
     * @param reusable whether to enable container reuse
     * @return this container instance
     * @throws IllegalStateException if {@code reusable} is {@code true} and managed mode is active
     */
    @Override
    public KibanaContainer withReuse(boolean reusable) {
        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");
        }

View on GitHub (pinned to 8e549514e3)