[英]@Value(“${local.server.port}”) not working in Spring boot 1.5
[英]Unable to set runtime Local Server Port in Spring Boot Test 1.5
我正在為我的應用程序使用 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>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.