简体   繁体   中英

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. 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). 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?

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

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.

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

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

Here ActiveProfiles does nothing. 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 . However both SpringBootTest and ContextConfiguration will attempt to configure the application context. Did you mean to add TestHelper to classes ?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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