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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/43491893/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 07:28:49  来源:igfitidea点击:

Unable to set runtime Local Server Port in Spring Boot Test 1.5

javaspringtestingspring-bootwebserver

提问by Meena Chaudhary

I am using Spring Boot 1.5 for my application. In integration testing I want to fetch the runtime port number of the web server(note: TestRestTemplate is not useful in my case.). There are a few approaches I have tried but none of them seem to work. Below are my approaches.

我正在为我的应用程序使用 Spring Boot 1.5。在集成测试中,我想获取 Web 服务器的运行时端口号(注意:TestRestTemplate 在我的情况下没有用。)。我尝试了几种方法,但它们似乎都不起作用。下面是我的方法。

First Approach

第一种方法

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

@LocalServerPort      
protected int port;

In my src/main/resources/config/application.propertiesfile I have defined server port as

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

server.port = 8081

服务器端口 = 8081

But With this code I am getting error

但是使用此代码我收到错误

Could not resolve placeholder 'local.server.port' in value "${local.server.port}"

无法解析值“${local.server.port}”中的占位符“local.server.port”

Second Approach

第二种方法

I have changed

我改变了

webEnvironment =WebEnvironment.DEFINED_PORT

webEnvironment =WebEnvironment.DEFINED_PORT

to

webEnvironment =WebEnvironment.RANDOM_PORT

webEnvironment =WebEnvironment.RANDOM_PORT

and in my src/main/resources/config/application.propertiesfile I have defined

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

server.port = 0

服务器端口 = 0

This throws the same as error as the first approach.

这与第一种方法抛出的错误相同。

Third Approach

第三种方法

In third approach I have tried to use

在我尝试使用的第三种方法中

protected int port;

@Autowired
Environment environment

this.port = this.environment.getProperty("local.server.port");

this returns nullvalue

这返回null

Fourth Approach

第四种方法

Lastly I have tried to use ApplicationEventsto find out the port number by creating an event listener to listen to EmbeddedServletContainerIntialize

最后,我尝试ApplicationEvents通过创建一个事件侦听器来侦听端口号EmbeddedServletContainerIntialize

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

public int getPort() {
return this.port;
} 

Added the same to TestConfig

添加了相同的 TestConfig

Now, In my test class I have tried use this listener to get the port

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

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

protected int port;

@Autowired
EmbeddedServletContainerIntializedEventListener embeddedServletcontainerPort;

this.port = this.embeddedServletcontainerPort.getPort();

this returns 0. Also, I have found out listener event is never triggered.

这返回0。此外,我发现从未触发侦听器事件。

It is very straight forward as in the docs and other posts but somehow It is not working for me. Help is much appreciated.

它在文档和其他帖子中非常简单,但不知何故它对我不起作用。非常感谢帮助。

回答by Kevin Peters

Maybe you forgot to configure the random port for your test web environment.

也许您忘记为您的测试 Web 环境配置随机端口。

This should do the trick: @SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)

这应该可以解决问题: @SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)

Here a test just executed successfully with Spring Boot 1.5.2:

这是一个刚刚使用 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));
    }

}

回答by Kin Cheung

You forgot to put @RunWith(SpringRunner.class)above your class decoration.

你忘了把@RunWith(SpringRunner.class)你的班级装饰放在上面。

So, try this.

所以,试试这个。

@RunWith(SpringRunner.class)
@SpringBootTest(classes = TestConfig.class, webEnvironment =WebEnvironment.DEFINED_PORT)
public class RestServiceTest {
     @LocalServerPort
     int randomServerPort;
     ...
}