简体   繁体   English

如何在集成测试运行之前加载环境变量以加载应用程序上下文

[英]How do I load environment variables to load application context before integration test runs

I'm working on multi-module maven project using Spring Boot 2.4.0.我正在使用 Spring Boot 2.4.0 开发多模块 maven 项目。 I have written integration tests for a module.我已经为一个模块编写了集成测试。 The test class looks similar to this.测试 class 看起来与此类似。

@RunWith(SpringRunner.class)
@SpringBootTest(classes = SpringApplicationClassWithMainMethod.class)
public class XYZServiceIT {

@Test
public void test1() {...}

@Test
public void test2() {...}
}

To run the SpringApplicationClassWithMainMethod.class ie, in order to load the Application context I need few environment variables that I set in eclipse.要运行 SpringApplicationClassWithMainMethod.class 即,为了加载应用程序上下文,我需要在 eclipse 中设置的几个环境变量。 So, in order to run the above integration test while loading the SpringApplicationClassWithMainMethod.class I need those environment variables before the Application context is loaded.因此,为了在加载 SpringApplicationClassWithMainMethod.class 时运行上述集成测试,我需要在加载应用程序上下文之前使用这些环境变量。

Trial-1: I have tried using @TestPropertySource(properties = {"key1=val1", "key2=val2"}) annotation, that didn't work.试用 1:我尝试使用 @TestPropertySource(properties = {"key1=val1", "key2=val2"}) 注释,但没有用。

Trial-2: I have also tried the static block to set environment variables which didnt work.试验 2:我还尝试了 static 块来设置不起作用的环境变量。

Trial-3: I also tried using the @ContextConfiguration with a ApplicationContextInitializer class, that didnt work as well.试验 3:我还尝试将 @ContextConfiguration 与 ApplicationContextInitializer class 一起使用,但效果不佳。

All these attempts to build the project using maven only lead to a所有这些使用 maven 构建项目的尝试只会导致

IllegalState Failed to load ApplicationContext IllegalState 无法加载 ApplicationContext

error for the above test class.上述测试 class 的错误。 Is there any way I could load the environment variables before the Application context gets loaded?有什么方法可以在加载应用程序上下文之前加载环境变量?

I think the correct class naming convention for Integration tests in maven would be XYZServiceIT since *Test is reserved for unit tests already which are run before application context.我认为 maven 中集成测试的正确 class 命名约定将是XYZServiceIT ,因为*Test是为已经在应用程序上下文之前运行的单元测试保留的。 You can change that if needed in your maven pom or simply stick with the conventional naming.如果需要,您可以在 maven pom 中更改它,或者简单地使用传统命名。

UPDATE更新

To pass environment variables to maven for your integration test use the following:要将环境变量传递给 maven 以进行集成测试,请使用以下命令:

  1. make sure you installed M2E from eclipse marketplace (found in menu > help > eclipse marketplace)确保您从 eclipse 市场安装了 M2E(在菜单 > 帮助 > eclipse 市场中找到)

来自市场的 m2e 插件

  1. right click your project > Run As... > 4 Maven Build... PS: afterwards you can find your run configuration at the top underneath the dropdown of the green arrow and in the run configuration settings if you need to rerun the tests in the future右键单击您的项目 > 运行方式... > 4 Maven 构建... PS:之后,您可以在绿色箭头下拉菜单下方的顶部找到您的运行配置,如果您需要在运行配置设置中重新运行测试未来

从右键单击项目的第一个 Maven 构建

  1. configure maven environment parameters either inline (for the maven goal command) with verify -Dkey=val or in the bottom variable section.使用verify -Dkey=val或在底部变量部分内联配置 maven 环境参数(对于 maven 目标命令)。 both work for unit and integrationtest.都适用于单元测试和集成测试。 environment typically does NOT work for test stage.环境通常不适用于测试阶段。 (If you don't have an JDK as the runner you will get an error. Follow this post to fix it if needed: No compiler is provided in this environment. Perhaps you are running on a JRE rather than a JDK? ) (如果您没有 JDK 作为运行程序,您将收到错误。如果需要,请按照此帖子修复它: 此环境中未提供编译器。也许您在 JRE 而不是 JDK 上运行?

带有环境变量的 mvn 目标设置

I hope that helps.我希望这会有所帮助。 You can also change pom.xml profiles if needed but i wouldn't recommend that.如果需要,您还可以更改 pom.xml 配置文件,但我不建议这样做。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何获取 SpringBatch 集成测试来加载我的应用程序属性? - How do I get a SpringBatch Integration test to load my application properties? 如何加载集成测试数据 - How to load integration test data 如何将 bean 加载到测试上下文中? - How to load beans into test context? 如何加载Spring Application Context - How to load Spring Application Context 如何更正 Spring 引导单元测试无法在两个几乎相同的测试类之一中加载应用程序上下文? - How can I correct Spring Boot unit test failed to load application context in one of two nearly identical test classes? 如何将bean加载到Spring MVC的应用程序上下文中? - How do you load a bean into Spring MVC's application context? 如何在Fitnesse测试中加载自定义比较器 - How do I load a customcomparator in a fitnesse test 如何在AWS Elastic Beanstalk环境中配置负载均衡器? - How do I configure a load balancer in an AWS Elastic Beanstalk environment? 如何自动加载servlet-context.xml? - How do I load servlet-context.xml automatically? 如何在web应用程序中初始化bean之前从zookeeper将属性加载到环境中 - How to load properties into environment from zookeeper before initialising beans in web application
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM