How can access the local.server.port when SpringBootTest.WebEnvironment.RANDOM_PORT in test

Viewed 254

I am trying to setup feign client for Spock test with in the test of the spring boot application.

The Spock test is setup with

@ActiveProfiles("functional-test")
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)

I have a application-functional-test.yml where my feign client is url define, I tried:

my.feign.client.example.url: localhost:${local.server.port}

But local server port says 0, which doesn't fit the random port for the application.

I also tried this where randomServerPort contains the random port, but I can't override the property:

 @LocalServerPort
 int randomServerPort;

 @Value('${my.feign.client.example.url}')
 String feignTestClient

 void setup() {
     feignTestClient="localhost:${randomServerPort}"
 }

Any ideas for a best practice? I prefer that the url is setup within application-functional-test.yml so avoid initializing with dummy value

My Feign client look like this:

@FeignClient(name = "myClient", url = "${my.feign.client.example.url}")
public interface FeignTestRestClient extends SomeControllerApi {}
0 Answers
Related