如何解析具有 @TestPropertySource 值的属性占位符?

问题描述 投票:0回答:1

我在测试中无法使用

@TestPropertySource
值解析占位符

这是 MRE:

import jakarta.annotation.PostConstruct;
import lombok.Getter;
import lombok.Setter;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
@ConfigurationProperties(prefix = "my-app")
public class GatewayMeta {
    @Value("${server.port}")
    private String port;

    @Setter
    @Getter
    private List<Server> servers;

    @PostConstruct
    private void init() {
        if (servers == null) {
            servers = List.of("http://localhost:" + port);
        }
    }
}
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.TestPropertySource;
import org.springframework.test.context.junit.jupiter.SpringExtension;

import java.util.List;

import static org.assertj.core.api.Assertions.assertThat;

@ExtendWith(SpringExtension.class)
@ContextConfiguration(classes = ServerPropertiesTest.ServerPropertiesTestConfig.class)
class ServerPropertiesTest {
    @Autowired
    ServerProperties serverProperties;
    @Test
    void ifNoServerSpecified_serversHasOneServerMatchingLocalhostServerPort() {
        List<String> servers = serverProperties.getServers();
        assertThat(servers).hasSize(1);
        String server = servers.get(0);
        assertThat(server).isEqualTo("http://localhost:8080");
    }

    @Configuration
    @EnableConfigurationProperties(ServerProperties.class)
    @Import(PropertySourcesPlaceholderConfigurer.class)
    @TestPropertySource(properties = "server.port=8080")
    static class ServerPropertiesTestConfig {
    }
}
Caused by: java.lang.IllegalArgumentException: Could not resolve placeholder 'server.port' in value "${server.port}"

我还尝试在

.properties
中指定一个
locations
文件(也许,
properties
只能丰富已有的属性?)

@TestPropertySource(locations = "classpath:application-test.properties",
        properties = "server.port=8080")

无论

*.properties
是否包含密钥,结果都是相同的。看来
PropertySourcesPlaceholderConfigurer
根本不关心我的
@TestPropertySource

我也尝试过这个:

    @Configuration
    @EnableConfigurationProperties(ServerProperties.class)
    @Import(PropertySourcesPlaceholderConfigurer.class)
    @PropertySource("classpath:application-test.properties") // has the property set to 8080
    @TestPropertySource(properties = "server.port=8000") // will it override?
    static class GatewayMetaTestConfig {
    }

也不例外,但是

@TestPropertySource
被完全忽略,并且其值不会覆盖
.properties
文件中指定的值

我需要导入一些 *PostProcessor(不是

@TestPropertySource
)的
other
PropertySourcesPlaceholderConfigurer
吗?

我访问了这些类似的帖子,但它们似乎没有帮助:123

我的错误是什么?

java spring testing
1个回答
0
投票

由于我不理解,

@TestProperySource
应该在你的测试类,而不是测试配置

此外,你不需要常规的

@PropertySource
,甚至,最让我惊讶的是,
PropertySourcesPlaceholderConfigurer

这是正确的版本:

@ExtendWith(SpringExtension.class)
@ContextConfiguration(classes = ServerPropertiesTest.ServerPropertiesTestConfig.class)
@TestPropertySource(properties = "server.port=8000")
class ServerPropertiesTest {
    @Autowired
    ServerProperties serverProperties;
    @Test
    void ifNoServerSpecified_serversHasOneServerMatchingLocalhostServerPort() {
        List<String> servers = serverProperties.getServers();
        assertThat(servers).hasSize(1);
        String server = servers.get(0);
        assertThat(server).isEqualTo("http://localhost:8080");
    }

    @Configuration
    @EnableConfigurationProperties(ServerProperties.class)
    static class ServerPropertiesTestConfig {
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.