在测试SpringBootTest.WebEnvironment.RANDOM_PORT时如何访问local.server.port

问题描述 投票:0回答:1

我正在尝试在Spring Boot应用程序的测试中为Spock测试设置伪装客户端。

Spock测试已通过]进行设置>

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

我有一个我的伪装客户端是url定义的application-functional-test.yml,我尝试过:

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

但是本地服务器端口显示为0,这不适合应用程序的随机端口。

我也尝试过这个randomServerPort包含随机端口,但是我不能覆盖该属性:

 @LocalServerPort
 int randomServerPort;

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

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

关于最佳做法的任何想法?我更喜欢在application-functional-test.yml中设置url,因此避免使用伪值初始化

我正在尝试在Spring Boot应用程序的测试中为Spock测试设置伪装客户端。 Spock测试是使用@ActiveProfiles(“ functional-test”)@SpringBootTest(webEnvironment = ...

spring spring-boot spock spring-cloud-feign
1个回答
0
投票

尝试在您的ApplicationContext中使用以下@Bean,并在测试中将其自动连接以能够调用portProvider.getPort()

© www.soinside.com 2019 - 2024. All rights reserved.