Skip to content

[Bug]: Keycloak testcontainer does not import the realm file #11374

@smaelc

Description

@smaelc

Module

Core

Testcontainers version

latest

Using the latest Testcontainers version?

Yes

Host OS

MacOs

Host Arch

ARM

Docker version

Server: Docker Desktop 4.55.0 (213807)
 Engine:
  Version:          29.1.3
  API version:      1.52 (minimum version 1.44)
  Go version:       go1.25.5
  Git commit:       fbf3ed2
  Built:            Fri Dec 12 14:50:40 2025
  OS/Arch:          linux/arm64
  Experimental:     false

What happened?

I have an issue with Keycloak testcontainer.

I still have the same error when trying fetch into my testcontainer., keycloak.
The error: 404 Not Found realm 'test-realm'.

Help please

public String getAccessToken(final String username, final String password) throws URISyntaxException {

		WebClient webClient = WebClient.builder().build();
		URI URI = new URIBuilder(keycloakContainer.getAuthServerUrl() + REALM_NAME + REALM_PROTOCOL_TOKEN).build();
		MultiValueMap<String, String> formData = new LinkedMultiValueMap<>();

		formData.put(OAuth2Constants.GRANT_TYPE, singletonList(OAuth2Constants.PASSWORD));
		formData.put(OAuth2Constants.CLIENT_ID, singletonList(CLIENT_ID));
		formData.put(OAuth2Constants.CLIENT_SECRET, singletonList(CLIENT_SECRET));
		formData.put("username", singletonList(username));
		formData.put("password", singletonList(password));

		return webClient.post()
				.uri(URI)
				.contentType(MediaType.APPLICATION_FORM_URLENCODED)
				.bodyValue(formData)
				.retrieve()
				.bodyToMono(TokenResponse.class)
				.map(TokenResponse::accessToken).block()
		;
	}

URI: http://localhost:52194/realms/test-realm/protocol/openid-connect/token

Relevant log output

org.springframework.web.reactive.function.client.WebClientResponseException$NotFound: 404 Not Found from POST http://localhost:52194/realms/test-realm/protocol/openid-connect/token

	at org.springframework.web.reactive.function.client.WebClientResponseException.create(WebClientResponseException.java:310)
	Suppressed: The stacktrace has been enhanced by Reactor, refer to additional information below: 
Error has been observed at the following site(s):
	*__checkpoint ⇢ 404 NOT_FOUND from POST http://localhost:52194/realms/test-realm/protocol/openid-connect/token [DefaultWebClient]

Additional Information

@Testcontainers
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@BeforeEach
void setUp(){
	testClient = RestTestClient.bindToServer()
			.baseUrl("http://localhost:"+port).build();
}
@Test
void shouldSuccessfullyGetPersistedElement_With_emailAddress() throws URISyntaxException {
	// ARRANGE
	String token = oAuth2Util.getAccessToken(ADMIN_USERNAME, ADMIN_PASSWORD);

	testClient.get().
			uri("/api/users/email/{emailAddress}", "demo@mail_test.edu")
			.headers(headers -> headers.setBearerAuth(token))
			.exchange()
			.expectStatus().isOk()
			.expectBody(User.class)
			.returnResult()
	;
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions