我的应用程序使用 Spring Boot 1.5。在集成测试中,我想获取 Web 服务器的运行时端口号(注意:TestRestTemplate 在我的情况下没有用。)。我尝试过几种方法,但似乎都不起作用。以下是我的方法。
第一种方法
@SpringBootTest(classes = TestConfig.class, webEnvironment =WebEnvironment.DEFINED_PORT)
public class RestServiceTest {
@LocalServerPort
protected int port;
在我的
src/main/resources/config/application.properties
文件中,我已将服务器端口定义为
服务器端口=8081
但是使用这段代码我遇到了错误
无法解析值“${local.server.port}”中的占位符“local.server.port”
第二种方法
我变了
webEnvironment =WebEnvironment.DEFINED_PORT
到
webEnvironment =WebEnvironment.RANDOM_PORT
并在我的
src/main/resources/config/application.properties
文件中定义了
服务器端口=0
这会引发与第一种方法相同的错误。
第三种方法
在第三种方法中我尝试使用
protected int port;
@Autowired
Environment environment
this.port = this.environment.getProperty("local.server.port");
这会返回
null
值
第四种方法
最后,我尝试使用
ApplicationEvents
通过创建事件侦听器来侦听 EmbeddedServletContainerIntialize
来找出端口号
@EventListener(EmbeddedServletContainerInitializedEvent.class)
public void onApplicationEvent(EmbeddedServletContainerInitializedEvent event) {
this.port = event.getEmbeddedServletContainer().getPort();
}
public int getPort() {
return this.port;
}
将相同内容添加到
TestConfig
现在,在我的测试类中,我尝试使用此侦听器来获取端口
@SpringBootTest(classes = TestConfig.class, webEnvironment =WebEnvironment.RANDOM_PORT)
public class RestServiceTest {
protected int port;
@Autowired
EmbeddedServletContainerIntializedEventListener embeddedServletcontainerPort;
this.port = this.embeddedServletcontainerPort.getPort();
这会返回
0
。另外,我发现监听器事件永远不会被触发。
就像文档和其他帖子中一样非常直接,但不知何故它对我不起作用。非常感谢您的帮助。
也许您忘记为测试 Web 环境配置随机端口。
这应该可以解决问题:
@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
这是刚刚使用 Spring Boot 1.5.2 成功执行的测试:
import static org.hamcrest.Matchers.greaterThan;
import static org.junit.Assert.assertThat;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
public class RandomPortTests {
@Value("${local.server.port}")
protected int localPort;
@Test
public void getPort() {
assertThat("Should get a random port greater than zero!", localPort, greaterThan(0));
}
}
我在使用 Spring Boot 1.4 的应用程序中遇到了同样的问题,发现
EmbeddedServletContainerInitializedEvent
的事件有点延迟 - 这意味着它在我的 bean 初始化后被触发 - 所以为了解决这个问题,我需要使用惰性需要使用端口的 bean 上的注释(例如 RestClient bean)并且它有效。
示例:
@Bean
@Lazy(true)
public RESTClient restClient() {
return new RESTClient(URL + port)
}
您忘记将
@RunWith(SpringRunner.class)
放在班级装饰上方。
所以,试试这个。
@RunWith(SpringRunner.class)
@SpringBootTest(classes = TestConfig.class, webEnvironment =WebEnvironment.DEFINED_PORT)
public class RestServiceTest {
@LocalServerPort
int randomServerPort;
...
}
我也有同样的问题。添加此依赖项解决了问题:
<!-- https://mvnrepository.com/artifact/io.cucumber/cucumber-spring -->
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-spring</artifactId>
<version>6.2.2</version>
</dependency>