![](/img/trans.png)
[英]Unable to set runtime Local Server Port in Spring Boot Test 1.5
[英]@Value(“${local.server.port}”) not working in Spring boot 1.5
我正在将Spring Boot从1.3升级到1.5。 升级到1.5我已经更换了
@SpringApplicationConfiguration(classes = TestConfig.class) @WebIntegrationTest
同
@SpringBootTest(classes = TestConfig.class)
另外,我正在使用
@Value("${local.server.port}") protected int port;
获取application.properties
文件中定义的端口号。 我进一步使用此端口号来构建REST URL。
但升级后我得到了下面的错误,而1.3
弹簧启动测试同样正常。
引起:java.lang.IllegalArgumentException:无法在org.springframework.util.PropertyPlaceholderHelper.parseStringValue(PropertyPlaceholderHelper.java:174)的值“$ {local.server.port}”中解析占位符'local.server.port'
我错过了为此工作需要做的任何更改。
您必须为webEnvironment
提供值。 在你的情况下DEFINED_PORT就像这样
@SpringBootTest(classes = App.class, webEnvironment = WebEnvironment.DEFINED_PORT)
public class YourTest {
@LocalServerPort // shorthand for @Value("${local.server.port}")
private Integer port;
...
}
有关详细信息,请参阅: https : //docs.spring.io/spring-boot/docs/current/reference/html/boot-features-testing.html#boot-features-testing-spring-boot-applications
对我来说问题是在我的其他测试中有替代的@Configuration
类,如下所示:
@Configuration
public class ReadPropertiesConfiguration {
@Bean
PropertyPlaceholderConfigurer propConfig() {
PropertyPlaceholderConfigurer placeholderConfigurer = new PropertyPlaceholderConfigurer();
placeholderConfigurer.setLocation(new ClassPathResource("application.properties"));
return placeholderConfigurer;
}
}
由于@ComponentScan,应用程序的@SpringBootApplication
正在挑选它,并且由于某种原因导致了这个问题。 当为这些添加排除和/或用其他解决方案替换它们时,事情再次开始工作而没有问题。
我不知道为什么会发生这种情况的根本原因,但这也可能是你的问题。
首先确保属性文件中的属性拼写正确。 正如我几天前使用spring-boot-1.5.2做的那样它可以工作。
要么
你需要添加
@PropertySource( “类路径:application.properties”)
到你的班级,所以它会选择你的配置。
如果您需要不同的测试配置,可以添加
@TestPropertySource(位置= “类路径:test.properties”)
添加我在别处的另一个替代解决方案。
我配置了
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
和
@RunWith(SpringRunner.class)
@SpringBootTest(classes = App.class, webEnvironment = WebEnvironment.RANDOM_PORT)
public class YourTest {
@LocalServerPort // shorthand for @Value("${local.server.port}")
private Integer port;
...
}
想到这一点,即使在指定Web环境等时仍然会出现此错误。我的${local.server.port}
似乎始终为null。
过了一段时间,我注意到我的Spring Boot启动消息没有包含它正在使用的端口的概念,所以显然它根本没有听任何端口 - 这解释了为什么它首先是null。 添加实际的容器实现依赖:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jetty</artifactId>
</dependency>
导致这出现在我的日志中:
2019-02-26 18:45:47.231 INFO 12504 --- [ main] o.s.b.web.embedded.jetty.JettyWebServer : Jetty started on port(s) 43132 (http/1.1) with context path '/'
之后local.server.port和@LocalServerPort也可以工作。
这个问题的另一个常见原因。
我的班级路径中有这种测试类
import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;
@Configuration
public class ReadTestPropertiesConfiguration {
@Bean
PropertyPlaceholderConfigurer propConfig() {
PropertyPlaceholderConfigurer placeholderConfigurer = new PropertyPlaceholderConfigurer();
placeholderConfigurer.setLocations(
new ClassPathResource("application.properties"),
new ClassPathResource("application-test.properties"));
return placeholderConfigurer;
}
}
它没有被明确地使用,但无论如何都被SpringRunner选中,只用属性文件中的值覆盖所有“普通”属性。 从类路径中删除此类解决了该问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.