Java Spring Boot OpenFeign随机端口测试

Java Spring Boot OpenFeign随机端口测试,java,spring-boot,spring-boot-test,openfeign,Java,Spring Boot,Spring Boot Test,Openfeign,我有一个OpenFeign客户端,设置如下: @FeignClient(name = "myService", qualifier = "myServiceClient", url = "${myservice.url}") public interface MyServiceClient { ... } @SpringBootTest(webEnvironment = RANDOM_PORT, classes = MyApplication.class) @RunWith(SpringRun

我有一个OpenFeign客户端,设置如下:

@FeignClient(name = "myService", qualifier = "myServiceClient", url = "${myservice.url}")
public interface MyServiceClient {
...
}
@SpringBootTest(webEnvironment = RANDOM_PORT, classes = MyApplication.class)
@RunWith(SpringRunner.class)
@EnableFeignClients(clients = MyServiceClient .class)
public class ReservationSteps {
...
}
弹簧启动测试设置如下:

@FeignClient(name = "myService", qualifier = "myServiceClient", url = "${myservice.url}")
public interface MyServiceClient {
...
}
@SpringBootTest(webEnvironment = RANDOM_PORT, classes = MyApplication.class)
@RunWith(SpringRunner.class)
@EnableFeignClients(clients = MyServiceClient .class)
public class ReservationSteps {
...
}
测试应该使应用程序加速,并使用外部客户端向其发送请求

问题在于随机_端口值

如何在属性文件中声明“myservice.url”属性,使其包含正确的端口

我试过这个:

myservice.url=localhost:${local.server.port}
但它会导致“localhost:0”

我不想对端口使用常量值


请帮忙。谢谢

我知道这是一个老问题,但也许这个答案会对某人有所帮助


作为一种解决方法,我们可以做的是让主机解析到Spring Ribbon。然后在测试开始之前动态配置主机

首先,如果您还没有maven依赖项,那么添加它


org.springframework.cloud
spring cloud starter netflix ribbon
测试
然后将测试配置为使用主机的“空”配置url运行,这是此处的
myservice.url
属性

@SpringBootTest(webEnvironment = RANDOM_PORT, classes = MyApplication.class)
@RunWith(SpringRunner.class)
@EnableFeignClients(clients = MyServiceClient.class)
@TestPropertySource(properties = "myservice.url=") // this makes sure we do the server lookup with ribbon
public class MyTest {
   ...
}
然后,在
@Before
方法中,我们需要做的就是向ribbon提供服务url,我们可以使用一个简单的
System.setProperty()

公共类MyTest{
@本地服务器端口
专用int端口;
@以前
公共作废设置(){
System.setProperty(“MyServiceClient.ribbon.listOfServers”http://localhost:“+港口);
...
}