Java 正在尝试为Spring Boot应用程序的cucumber集成测试加载不同的属性?

Java 正在尝试为Spring Boot应用程序的cucumber集成测试加载不同的属性?,java,spring,spring-boot,cucumber,integration-testing,Java,Spring,Spring Boot,Cucumber,Integration Testing,我正在尝试使用cucumber的不同属性为我的spring boot应用程序运行集成测试。如果我使用主应用程序类中加载的默认属性,我可以让它运行。但是,当我将@TestPropertySource指定给配置属性的这个不同位置时,它仍然使用主应用程序类的应用程序上下文。因此,测试执行时的应用程序上下文与应用程序在服务器上运行时的上下文相同。我不要这个 这是一个使用Spring Boot 1.5.18和Cucumber 4.2.2的与工作相关的web应用程序 对于我的所有java类和包,目录结构是s

我正在尝试使用cucumber的不同属性为我的spring boot应用程序运行集成测试。如果我使用主应用程序类中加载的默认属性,我可以让它运行。但是,当我将@TestPropertySource指定给配置属性的这个不同位置时,它仍然使用主应用程序类的应用程序上下文。因此,测试执行时的应用程序上下文与应用程序在服务器上运行时的上下文相同。我不要这个

这是一个使用Spring Boot 1.5.18和Cucumber 4.2.2的与工作相关的web应用程序

对于我的所有java类和包,目录结构是src/main/java,src/main/resources带有application.properties和其他一些,是一个根级别的文件夹,带有环境日志和安全属性。然后我有src/test/java和cucumber代码,src/test/resources和修改后的application.properties文件,我想在测试执行时使用它们。我还想为测试指定一个不同的环境、安全性、日志配置属性文件

这是我的ApplicationTest.Java类,我试图在其中使用不同的属性源

@RunWith(SpringRunner.class)
@SpringBootTest(classes = Application.class, webEnvironment = 
SpringBootTest.WebEnvironment.RANDOM_PORT)
@TestPropertySource( value = 
{"file:${env.dir}/deploy/acceptance/config/env.properties",
    "file:${env.dir}/deploy/acceptance/config/AppConfig.properties",
    "file:${env.dir}/deploy/acceptance/security/auth.properties", 
"classpath:application-test.properties"})
public abstract class ApplicationTest {
    public ApplicationTest() {
}
这是我的Cucumber4Test.Java类

@RunWith(Cucumber.class)
@CucumberOptions(features = "src/test/resources", 
    plugin = {"pretty", "html:target/cucumber", 
"junit:target/reports/cucumber.json"}, glue = { "com.test.packages.cucumber" 
}, monochrome = true)

public class CucumberTest {

}
我不确定我是否在这些课程中遗漏了什么,因为我遵循了一个教程。但是正如我所说的,如果我没有在ApplicationTest类中设置任何属性源,并且在eclipse中作为junit运行CucumberTest.java,或者运行mvn clean install、mvn test等,cucumber测试将按预期执行

我已经在这里搜索了很多问题,尝试了很多事情,但似乎没有什么对我有用

编辑:我认为@TestPropertySource无法工作的原因是:Spring中的属性源优先级。当我在src/test/java中加载cucumber时,它会加载我指定的那些属性,但随后它会在src/main/java文件夹中启动应用程序。这里是在Application.java中加载默认属性。Spring文档说,最后加载的属性优先,因此当应用程序启动时,我的TestPropertySource将被覆盖

我的工作解决方案是:我想让cucumber在Jenkins中独立于构建和部署管道运行。但无法找到绕过我的工作的配置和属性的路径和目录结构标准的方法。所以我所做的是:

1) 将我需要的属性添加到src/test/resources中的类路径中

@RunWith(SpringRunner.class)
@SpringBootTest(classes = Application.class, webEnvironment = 
SpringBootTest.WebEnvironment.RANDOM_PORT)
@TestPropertySource( value = 
{"file:${env.dir}/deploy/acceptance/config/env.properties",
    "file:${env.dir}/deploy/acceptance/config/AppConfig.properties",
    "file:${env.dir}/deploy/acceptance/security/auth.properties", 
"classpath:application-test.properties"})
public abstract class ApplicationTest {
    public ApplicationTest() {
}
2) 现在,这有点老套,但是src/test/java中的第二个Application.java使用@Propertysources反映了我想要使用的属性

3) 在jenkins中,我在运行mvn测试之前执行预构建步骤。这个shell只是将src/test/java/package/with/Application.java移动到src/main/java/package/with/Application.java中。这将用不同的属性覆盖通常的Application.java类

4) 运行mvn测试

5) 利润

这很有效

使用默认应用程序

package cucumber.examples.spring.txn;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;

@SpringBootApplication
@EnableWebMvc
public class Application {

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }

}
以及一些
application.properties

key=App
然后运行:

package cucumber.examples.spring.txn;

import cucumber.api.junit.Cucumber;
import org.junit.runner.RunWith;

@RunWith(Cucumber.class)
public class RunCukesTest {
}
并使用此测试上下文配置

package cucumber.examples.spring.txn;

import cucumber.api.java.Before;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.TestPropertySource;

@SpringBootTest
@AutoConfigureMockMvc
public class CucumberContextConfiguration  {

    @Value("${key}")
    private String value;

    @Before
    public void setup_cucumber_spring_context(){
        // Dummy method so cucumber will recognize this class as glue
        // and use its context configuration.

        System.out.println("Property was " + value);
    }
}
将打印
属性为App

@TestPropertySource(“classpath:test.properties”)
添加到
CucumberContextConfiguration
并创建一个
test.properties
文件,其中包含

key=Test

将打印
属性was Test

我在
ApplicationTest
类中没有看到步骤定义。如果没有一个台阶或弯钩的定义,cucumber将忽略它。您可能需要添加一个虚拟步骤。@mpkorstanje谢谢。实际上,我有一个从应用程序测试继承的公共steps def类。我不认为这就是为什么当应用程序在application.java中启动时,测试抗议者没有得到尊重的原因。您应该尝试创建一个最小的示例。你可以以此为基础:这样做对我来说很有效。我将在下面发布详细信息。谢谢,但由于工作中的标准,我无法更改Application.java中的属性源。当我们部署到服务器上,并且需要中间件团队创建新实例时,它们会以这种方式预先确定目录结构和位置,从而使部署更容易。这是我的问题,我的解决方案是我的解决办法。改变标准需要更多的工作和多个团队来改变他们做事的方式。我真的很感谢你的帮助。