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

问题描述 投票:1回答:1

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

这是使用Spring Boot 1.5.18,Cucumber 4.2.2的与工作相关的Web应用程序,

对于我所有的Java类和包,目录结构都是src / main / java,带有application.properties和其他一些目录的src / main / resources,具有环境日志记录和安全性属性的根目录文件夹。然后我有src / test / java和我的黄瓜代码,而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中将jcumet作为junit运行CucumberTest.java,或运行mvn clean install,mvn test等,则黄瓜测试将按预期执行。

我在这里搜索了很多问题,尝试了很多事情,但似乎没有任何效果对我有用。 Override @PropertySource with @TestPropertySource in Spring Boot

Load different application.yml in SpringBoot Test

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

我的工作解决方案:我想让黄瓜在Jenkins中运行,这是与我们的构建和部署管道分开的工作。但是找不到围绕我的工作的配置和属性的路径和目录结构标准的方法。所以我做了:

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

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)利润

java spring spring-boot cucumber integration-testing
1个回答
1
投票

这有效。

使用默认应用程序。

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

© www.soinside.com 2019 - 2024. All rights reserved.