java - 正确配置 Spring Boot 2 和 JUnit 5

标签 java spring maven spring-boot junit5

使用 Spring Boot 2.0.0.RC2。

我写了一个配置类:

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

    @Bean
    public LDClientInterface ldClient(LDClientConfigurationProperties props) {
        return new LDClient(props.getSdkKey(), props.getLDConfig());
    }
}

还有一个 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();
    }
}

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

launchdarkly:
    sdkKey: <redacted>

我有以下依赖项:

    <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>

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

我似乎无法为我的测试类找出正确的注释来从 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());
    }
}

如果我用 @ExtendWith(SpringExtension.class) 注释类 和 @SpringBootTest 然后它尝试加载 com/company/spring/launchdarkly/LDClientConfigurationTest-context.xml 然后 com/company/spring/launchdarkly/LDClientConfigurationTestContext。 groovy 但不查找 yml 文件。

最佳答案

Spring Boot 2 和 JUnit 5 集成测试的正确注释集是(在 Kotlin 中):

@ExtendWith(SpringExtension::class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@ActiveProfiles("dev") // optional
@TestInstance(TestInstance.Lifecycle.PER_CLASS) // optional

您可以通过字段上的 @LocalServerPort 注释获取服务运行的端口。

关于java - 正确配置 Spring Boot 2 和 JUnit 5,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49018415/

相关文章:

java - Android工具栏: shadow on top instead of bottom

java - 在不同的显示器上测试失败 selenium webdriver java

java - 将 "<"或 ">"作为参数传递给比较器

spring - java配置中出现Weblogic jndi NameNotFoundException

java - 使用 DeadLetterPublishingRecoverer 和手动 ack 模式的 Spring Kafka 监听器

java - Maven:生成包含所有依赖项的可执行 jar 文件

maven - 如何在 Eclipse Oxygen 中将 log4j 与 maven 和 java9 一起使用?

java - 特定于平台的 Maven 依赖关系

java - LWJGL - 渲染周期屏幕闪烁

Spring mvc Controller 处理所有请求,不应该那样做