尝试为 Spring Boot App 的黄瓜集成测试加载不同的属性?

Trying to load different properties for cucumber integration test of Spring Boot App?

我正在尝试使用 Cucumber 的不同属性来 运行 对我的 spring 启动应用程序进行集成测试。如果我使用主应用程序 class 中加载的默认属性,我可以将它设置为 运行。但是,当我将 @TestPropertySource 指定到配置属性的这个不同位置时,它仍然使用主应用程序 class 的应用程序上下文。因此,它在测试执行时的应用程序上下文与应用程序在服务器上 运行ning 时的上下文相同。我不想要这个。

这是一个与工作相关的网络应用程序,使用 Spring Boot 1.5.18,Cucumber 4.2.2。

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

这是我的 ApplicationTest.Java class,我正在尝试使用不同的 属性 来源。

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

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

}

我不确定在学习教程时我是否遗漏了那些 classes 中的任何内容。但正如我所说,如果我没有在我的 ApplicationTest class 中设置任何 属性 源,并且 运行 将 CucumberTest.java 作为 eclipse 中的 junit,或者 运行 mvn clean install、mvn test 等,黄瓜测试将按预期执行。

我已经尝试了很多方法来搜索这里的问题,但似乎对我没有任何帮助。

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

我的工作解决方案:我想在 Jenkins 中将 Cucumber 从我们的构建和部署管道中分离出来,作为一个单独的工作。但是找不到绕过我的工作标准的配置和属性的路径和目录结构的方法。所以我做了什么:

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

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

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);
    }
}

将打印 Property was App.

@TestPropertySource("classpath:test.properties") 添加到 CucumberContextConfiguration 并创建包含

test.properties 文件
key=Test

将打印 Property was Test.