简体   繁体   English

使用 Junit 5 的 Spring Boot Cucumber 测试不会加载 application-test.properties

[英]Spring Boot Cucumber Test with Junit 5 will not load application-test.properties

I am not able to get my Cucumber test to read from the application-test.properties file, which resides in the test/resources folder.我无法让我的 Cucumber 测试从位于 test/resources 文件夹中的 application-test.properties 文件中读取。 The cucumber test is reading the features, but the application fails to start because the @Value annotation does not pick up the value from the application-test.properties file (value is null).黄瓜测试正在读取特征,但应用程序无法启动,因为 @Value 注释没有从 application-test.properties 文件中获取值(值为 null)。 So all the tests fail.所以所有的测试都失败了。 The cucumber tests were working fine with Junit 4. Why are my cucumber tests unable to read from the application-properties file?黄瓜测试在 Junit 4 上运行良好。为什么我的黄瓜测试无法从应用程序属性文件中读取?

In one of my application classes, I am using the @Value("${connection.string}") annotation to retrieve a hard-coded connection string in the application-test.properties file.在我的一个应用程序类中,我使用@Value("${connection.string}")注释来检索 application-test.properties 文件中的硬编码连接字符串。 The connection string is null, so the application fails to start due to an instantiation error.连接字符串为空,因此应用程序由于实例化错误而无法启动。

Properties file can be found at: test/resources/application-test.properties , target/test-classes/application-test.properties可以在以下位置找到属性文件: test/resources/application-test.propertiestarget/test-classes/application-test.properties

Here is my Application class:这是我的应用程序类:

@SpringBootApplication
@EntityScan(basePackages = { "com.sams.payout.models.dto" })
@ComponentScan(basePackages = { "com.sams.payout", "com.samcash" })
@Slf4j
public class SampleSpringApplication {

    public static void main(String[] args) {
        SpringApplication.run(SampleSpringApplication.class, args);
        log.info("Application Started");
    }
}

Here is the test app:这是测试应用程序:

@CucumberContextConfiguration
@SpringBootTest(classes = SampleSpringApplication.class)
@ContextConfiguration(classes = TestHelper.class)
@ActiveProfiles("test")
public class SampleTestApp {
}

Here is the functional test:下面是功能测试:

@Suite
@IncludeEngines("cucumber")
@SelectClasspathResource("features")
@ConfigurationParameters({
        @ConfigurationParameter(key = PLUGIN_PROPERTY_NAME, value = "pretty,json:target/cucumber.json"),
        @ConfigurationParameter(key = JUNIT_PLATFORM_NAMING_STRATEGY_PROPERTY_NAME, value = "long")
})
@ActiveProfiles("test")
public class SampleFT {
}

There is nothing wrong with the cucumber tests as they work using Junit 4.黄瓜测试没有任何问题,因为它们使用 Junit 4 工作。

partial pom.xml:部分 pom.xml:

        <dependency>
            <groupId>au.com.dius.pact.provider</groupId>
            <artifactId>junit5</artifactId>
            <version>4.1.36</version>
        </dependency>

        <dependency>
            <groupId>org.assertj</groupId>
            <artifactId>assertj-core</artifactId>
            <scope>test</scope>
        </dependency>

        <!--Functional tests -->
        <dependency>
            <groupId>com.google.code.gson</groupId>
            <artifactId>gson</artifactId>
            <version>2.9.0</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>io.cucumber</groupId>
            <artifactId>cucumber-java</artifactId>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>io.cucumber</groupId>
            <artifactId>cucumber-junit-platform-engine</artifactId>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>io.cucumber</groupId>
            <artifactId>cucumber-spring</artifactId>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>net.masterthought</groupId>
            <artifactId>cucumber-reporting</artifactId>
            <version>${cucumber-reporting.version}</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>com.azure</groupId>
            <artifactId>azure-core</artifactId>
        </dependency>

        <dependency>
            <groupId>org.junit.platform</groupId>
            <artifactId>junit-platform-suite</artifactId>
            <scope>test</scope>
        </dependency>

Junit version is 5.8.1, spring-boot version is 2.6.7, and Cucumber version is 7.3.3, using BOMs. Junit 版本是 5.8.1,spring-boot 版本是 2.6.7,Cucumber 版本是 7.3.3,使用 BOM。

@Suite
@IncludeEngines("cucumber")
...
@ActiveProfiles("test")
public class SampleFT {
}

Here ActiveProfiles does nothing.这里ActiveProfiles什么都不做。 This class isn't used to build the test application context.此类不用于构建测试应用程序上下文。

@CucumberContextConfiguration
@SpringBootTest(classes = SampleSpringApplication.class)
@ContextConfiguration(classes = TestHelper.class)
@ActiveProfiles("test")
public class SampleTestApp {
}

This SampleTestApp is used to build the test application context because it is anotated with CucumberContextConfiguration .SampleTestApp用于构建测试应用程序上下文,因为它使用CucumberContextConfiguration进行了注释。 However both SpringBootTest and ContextConfiguration will attempt to configure the application context.但是SpringBootTestContextConfiguration都将尝试配置应用程序上下文。 Did you mean to add TestHelper to classes ?您的意思是将TestHelper添加到classes吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM