java - 尝试加载不同的属性以进行 Spring Boot 应用程序的 Cucumber 集成测试?

标签 java spring spring-boot cucumber integration-testing

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

这是一个使用 Spring Boot 1.5.18、Cucumber 4.2.2 的工作相关 Web 应用程序。

目录结构是 src/main/java ,包含我所有的 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 {

}

我不确定在我遵循教程时是否遗漏了这些类(class)中的任何内容。但正如我所说,如果我没有在 ApplicationTest 类中设置任何属性源,并在 Eclipse 中将 CucumberTest.java 作为 junit 运行,或者运行 mvn clean install、mvn test 等,则 Cucumber 测试将按预期执行。

我已经尝试了很多事情来搜索这里的问题,但似乎没有什么对我有用。 Override @PropertySource with @TestPropertySource in Spring Boot

Load different application.yml in SpringBoot Test

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

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

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

2)现在这有点 hacky,但是 src/test/java 中的第二个 Application.java 与 @Propertysources 反射(reflect)了我想要使用的属性。

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

将打印Property was App

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

test.properties文件>
key=Test

将打印Property was Test

关于java - 尝试加载不同的属性以进行 Spring Boot 应用程序的 Cucumber 集成测试?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54430684/

相关文章:

java - 加法答案错误

java - 在整个二叉搜索树中搜索

java - 使用另一个库将驱动程序路径发送到 selenium 节点

java - 对象为空,但应用程序的行为就像不是

spring - 如何使用 Spring Boot 在 H2 控制台中设置默认 JDBC URL 值

java - 在没有 http 请求的情况下,如何处理请求(套接字连接)bean 范围?

java - 使用 Java Cloudant API 存储 map

java - Spring JPA + Hibernate 搜索 : How update the Search Index(Lucene) only?

java - spring boot "Whitelabel Error Page"无可用信息

java - Hibernate 在 Spring Boot 1.4 开始时重命名了一些列