@Test
    public void elasticsearchSecuredTest() throws IOException {
        try (
            ElasticsearchContainer container = new ElasticsearchContainer(ELASTICSEARCH_IMAGE)
                .withPassword(ELASTICSEARCH_PASSWORD)
        ) {
            container.start();

            // The cluster should be secured so it must fail when we try to access / without credentials
            assertThat(catchThrowable(() -> getAnonymousClient(container).performRequest(new Request("GET", "/"))))
                .as("We should not be able to access / URI with an anonymous client.")
                .isInstanceOf(ResponseException.class);

            // But it should work when we try to access / with the proper login and password
            Response response = getClient(container).performRequest(new Request("GET", "/"));
            assertThat(response.getStatusLine().getStatusCode()).isEqualTo(200);
            assertThat(EntityUtils.toString(response.getEntity())).contains(ELASTICSEARCH_VERSION);
        }
    }
From 6.8, we can optionally activate security with a default password.
modules/elasticsearch/src/test/java/org/testcontainers/elasticsearch/ElasticsearchContainerTest.java