正确配置Spring Boot 2和JUnit 5

Eri*_*ric 5 java spring maven spring-boot junit5

使用Spring Boot 2.0.0.RC2。

我编写了一个Configuration类:

@Configuration
@ConditionalOnProperty("launchdarkly.sdkKey")
public class LDClientConfiguration  {

    @Bean
    public LDClientInterface ldClient(LDClientConfigurationProperties props) {
        return new LDClient(props.getSdkKey(), props.getLDConfig());
    }
}
Run Code Online (Sandbox Code Playgroud)

还有一个ConfigurationProperties类:

@Component
@ConfigurationProperties(prefix = "launchdarkly")
public class LDClientConfigurationProperties {

    private String sdkKey;
    // more attributes

    public void setSdkKey(String sdkKey) {
        this.sdkKey = sdkKey;
    }
    // more setters

    public LDConfig getLDConfig() {
        LDConfig.Builder builder = new LDConfig.Builder();
        // set builder w/ attributes
        return builder.build();
    }
}
Run Code Online (Sandbox Code Playgroud)

我正在尝试测试它,从src / test / resources / application-test.yml中读取配置:

launchdarkly:
    sdkKey: <redacted>
Run Code Online (Sandbox Code Playgroud)

我有以下依赖项:

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-test</artifactId>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-test</artifactId>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.junit.jupiter</groupId>
        <artifactId>junit-jupiter-api</artifactId>
        <version>${junit-jupiter.version}</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.junit.jupiter</groupId>
        <artifactId>junit-jupiter-engine</artifactId>
        <version>${junit-jupiter.version}</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.junit.jupiter</groupId>
        <artifactId>junit-jupiter-params</artifactId>
        <version>${junit-jupiter.version}</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.junit.platform</groupId>
        <artifactId>junit-platform-engine</artifactId>
        <version>${junit-platform.version}</version>
        <scope>test</scope>
    </dependency>
Run Code Online (Sandbox Code Playgroud)

并且在maven-surefire-plugin v2.19.1中配置了junit-platform-surefire-provider

我似乎无法找出适合我的Test类的注释,以从src / test / resources / application-test.yml中读取配置。

我已经阅读了https://junit.org/junit5/docs/current/user-guide,但似乎仍然无法正确注释。任何帮助,将不胜感激。

编辑:测试类

@SpringJUnitConfig(SpringBootContextLoader.class)
public class LDClientConfigurationPropertiesTest {

    @Autowired
    private LDClientConfigurationProperties props;

    @Test
    public void test() {
        LDConfig config = props.getLDConfig();
        assertThat(config, notNullValue());
    }
}
Run Code Online (Sandbox Code Playgroud)

如果我注释与类@ExtendWith(SpringExtension.class) 和@SpringBootTest然后它会尝试加载com/company/spring/launchdarkly/LDClientConfigurationTest-context.xml,然后com/company/spring/launchdarkly/LDClientConfigurationTestContext.groovy,但不寻找一个YML文件。

Eri*_*ric -4

我的测试类上的这组注释似乎有效:

@SpringBootApplication
@ExtendWith(SpringExtension.class)
@SpringBootTest(classes = { <array of classes goes here> })
Run Code Online (Sandbox Code Playgroud)