Replies: 2 comments 1 reply
-
Moved the issue to discussions, as this is an issue with your implementation, not with the project itself. |
Beta Was this translation helpful? Give feedback.
1 reply
-
Here is an example how to use @Testcontainers
@FlywayTestExtension
@DirtiesContext
@SpringBootTest(
webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT,
classes = [Application::class]
)
@ContextConfiguration(initializers = [BaseWebClientTest.Initializer::class])
abstract class BaseWebClientTest {
@Autowired
protected lateinit var webTestClient: WebTestClient
companion object {
@Container
val keycloak: KeycloakContainer = KeycloakContainer()
.withRealmImportFile("export_realm.json")
.withEnv("DB_VENDOR", "h2")
}
private lateinit var authServerUrl: String
class Initializer : ApplicationContextInitializer<ConfigurableApplicationContext> {
override fun initialize(configurableApplicationContext: ConfigurableApplicationContext) {
val values = TestPropertyValues.of(
"keycloak.auth-server-url=${keycloak.authServerUrl}",
"keycloak.realm=SpringBootKeycloak",
"keycloak.resource=backend-api",
"keycloak.bearer-only=true",
"keycloak.credentials.secret=secret"
)
values.applyTo(configurableApplicationContext)
}
}
@BeforeEach
internal fun setUp() {
this.authServerUrl = "${keycloak.authServerUrl}/realms/SpringBootKeycloak/protocol/openid-connect/token"
}
protected fun getAdminToken(): String {
return getToken("admin", "password")
}
protected fun getMemberToken(): String {
return getToken("member", "password")
}
protected fun getToken(username: String, password: String): String {
val restTemplate = RestTemplate()
val httpHeaders = HttpHeaders()
httpHeaders.contentType = MediaType.APPLICATION_FORM_URLENCODED
val map: MultiValueMap<String, String> = LinkedMultiValueMap()
map["grant_type"] = listOf("password")
map["client_id"] = listOf("backend-api")
map["client_secret"] = listOf("secret")
map["username"] = listOf(username)
map["password"] = listOf(password)
val token = restTemplate.postForObject(authServerUrl, HttpEntity(map, httpHeaders), KeyCloakToken::class.java)!!
return token.accessToken
}
private class KeyCloakToken @JsonCreator constructor(@JsonProperty("access_token") val accessToken: String)
} |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Hi,
Could you please help with the above error, I am not sure what I am doing wrong. Please ask if you want me to provide any more info/code
Initializer:
Test class:
application.properties file
Error
Beta Was this translation helpful? Give feedback.
All reactions