Home > Mobile >  How to use @LocalPort together with @DynamicPropertySource in a SpringBootTest?
How to use @LocalPort together with @DynamicPropertySource in a SpringBootTest?

Time:04-04

we have the following configuration

application-local.yaml

spring:
  security:
    oauth2:
      resourceserver:
        jwt:
          jwk-set-uri: http://localhost:8080/jwks

application-production.yaml

spring:
  security:
    oauth2:
      resourceserver:
        jwt:
          jwk-set-uri: https://auth.foo.com/.well-known/jwks.json

When running on localhost, we have a Controller in place which mocks the OAuth2 Endpoints and returning self-signed Tokens.

Everything is working as expected when using a static port.

We struggled to override the property jwt-set-uri with a dynamic port in a SpringBootTest while using WebEnvironment.RANDOM_PORT

According to the Docs @DynmicPropertSource should be a perfect fit but is required to be static (meaning part of the companion object in Kotlin)

@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
class Test {

@LocalServerPort
private var port = 0

 companion object {
        @DynamicPropertySource
        fun registerUrl(registry: DynamicPropertyRegistry) {
            registry.add("spring.security.oauth2.resourceserver.jwt.jwk-set-uri", "???")
        }
    }
}

My question: I"m probably missing something here but how is it possible to access the non static port in the static context of @DynamicPropertySource ?

CodePudding user response:

Since @LocalServerPort is a property you can annotate it with @JvmStatic. You would then be able to use it in your @DynamicPropertySource

  • Related