无法在Spring Boot Test 1.5中设置运行时本地服务器端口

Mee*_*ary 6 java testing webserver spring spring-boot

我使用Spring Boot 1.5作为我的应用程序.在集成测试中,我想获取Web服务器的运行时端口号(注意:在我的情况下,TestRestTemplate没用).我尝试了一些方法,但似乎没有一种方法可行.以下是我的方法.

第一种方法

@SpringBootTest(classes = TestConfig.class, webEnvironment =WebEnvironment.DEFINED_PORT)
public class RestServiceTest {

@LocalServerPort      
protected int port;
Run Code Online (Sandbox Code Playgroud)

在我的src/main/resources/config/application.properties文件中,我将服务器端口定义为

server.port = 8081

但是使用这段代码我会收到错误

无法解决值"$ {local.server.port}"中的占位符"local.server.port"

第二种方法

我改变了

webEnvironment = WebEnvironment.DEFINED_PORT

webEnvironment = WebEnvironment.RANDOM_PORT

在我的src/main/resources/config/application.properties文件中我定义了

server.port = 0

这与第一种方法的错误相同.

第三种方法

在第三种方法中,我试图使用

protected int port;

@Autowired
Environment environment

this.port = this.environment.getProperty("local.server.port");
Run Code Online (Sandbox Code Playgroud)

这会返回null

第四种方法

最后,我试图ApplicationEvents通过创建一个要监听的事件监听器来查找端口号EmbeddedServletContainerIntialize

@EventListener(EmbeddedServletContainerInitializedEvent.class)
public void onApplicationEvent(EmbeddedServletContainerInitializedEvent event) {
this.port = event.getEmbeddedServletContainer().getPort();
}

public int getPort() {
return this.port;
} 
Run Code Online (Sandbox Code Playgroud)

添加相同的 TestConfig

现在,在我的测试类中,我尝试使用此侦听器来获取端口

@SpringBootTest(classes = TestConfig.class, webEnvironment =WebEnvironment.RANDOM_PORT)
public class RestServiceTest {

protected int port;

@Autowired
EmbeddedServletContainerIntializedEventListener embeddedServletcontainerPort;

this.port = this.embeddedServletcontainerPort.getPort();
Run Code Online (Sandbox Code Playgroud)

这回来了0.另外,我发现侦听器事件永远不会被触发.

它在文档和其他帖子中非常直接但不知何故它对我不起作用.非常感谢帮助.

Kev*_*ers 9

也许您忘了为测试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));
    }

}
Run Code Online (Sandbox Code Playgroud)


小智 6

我在使用 spring boot 1.4 的应用程序中遇到了同样的问题,发现事件EmbeddedServletContainerInitializedEvent有点延迟 - 这意味着它在我的 bean 初始化后被触发 - 所以为了解决这个问题,我需要在 bean 上使用惰性注释需要使用端口作为例如 RestClient bean 并且它工作。例子:

@Bean
@Lazy(true)
public RESTClient restClient() {
   return new RESTClient(URL + port)
}
Run Code Online (Sandbox Code Playgroud)

  • 这通过在注入到“@RestController”的“@Component”中使用“@LocalServerPort”解决了相同的错误。小提示:“@Lazy(true)”可以简单地是“@Lazy”。 (2认同)