7

我正在将 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.3Spring Boot Test 中同样可以正常工作。

原因:java.lang.IllegalArgumentException:无法在 org.springframework.util.PropertyPlaceholderHelper.parseStringValue(PropertyPlaceholderHelper.java:174) 中解析值“${local.server.port}”中的占位符“local.server.port”

我是否错过了为此工作需要做的任何更改。

4

4 回答 4

10

您必须为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

于 2017-12-11T16:23:10.527 回答
2

添加我在其他地方拥有的另一个替代解决方案。

我已经配置

    <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}似乎总是为空。

一段时间后,我注意到我的 Spring Boot 启动消息不包含它正在使用的端口的概念,因此显然它根本没有监听任何端口 - 这解释了为什么它首先为空。添加实际的容器实现依赖:

    <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 也可以工作。

于 2019-02-26T16:56:36.527 回答
1

对我来说,问题是@Configuration在我的其他测试中有替代类,如下所示:

@Configuration
public class ReadPropertiesConfiguration {
    @Bean
    PropertyPlaceholderConfigurer propConfig() {
        PropertyPlaceholderConfigurer placeholderConfigurer = new PropertyPlaceholderConfigurer();
        placeholderConfigurer.setLocation(new ClassPathResource("application.properties"));
        return placeholderConfigurer;
    }
}

@SpringBootApplication由于它的@ComponentScan,该应用程序正在接受它,并且由于某种原因导致了这个问题。当为这些添加排除项和/或用其他解决方案替换它们时,事情又开始正常工作了。

我不知道发生这种情况的根本原因,但这也可能是您的问题。

于 2017-08-31T10:26:41.397 回答
0

首先确保属性在属性文件中拼写正确。正如我几天前使用 spring-boot-1.5.2 所做的那样,它可以工作。

或者

您需要添加

@PropertySource("classpath:application.properties")

到你的班级,所以它会选择你的配置。

如果您需要不同的配置进行测试,您可以添加

@TestPropertySource(locations="classpath:test.properties")

参考@Value 在 Spring Boot Test 上不起作用

于 2017-04-18T14:56:24.873 回答