如何在集成测试运行之前加载环境变量以加载应用程序上下文 [英] How do I load environment variables to load application context before integration test runs
问题描述
我正在使用 Spring Boot 2.4.0 开发多模块 Maven 项目.我已经为一个模块编写了集成测试.测试类看起来与此类似.
@RunWith(SpringRunner.class)@SpringBootTest(classes = SpringApplicationClassWithMainMethod.class)公共类 XYZServiceIT {@测试public void test1() {...}@测试public void test2() {...}}
要运行 SpringApplicationClassWithMainMethod.class,即为了加载应用程序上下文,我需要在 eclipse 中设置的几个环境变量.因此,为了在加载 SpringApplicationClassWithMainMethod.class 时运行上述集成测试,我需要在加载应用程序上下文之前使用这些环境变量.
试验 1:我曾尝试使用 @TestPropertySource(properties = {"key1=val1", "key2=val2"}) 注释,但没有用.
试验 2:我也试过静态块来设置环境变量,但没有用.
试验 3:我还尝试将 @ContextConfiguration 与 ApplicationContextInitializer 类一起使用,但效果不佳.
所有这些使用 maven 构建项目的尝试只会导致一个
<块引用>IllegalState 加载 ApplicationContext 失败
上述测试类的错误.有什么办法可以在加载应用程序上下文之前加载环境变量?
我认为 maven 中集成测试的正确类命名约定是 XYZServiceIT
因为 *Test
是为已经在应用程序上下文之前运行的单元测试保留.如果需要,您可以在您的 maven pom 中更改它,或者只是坚持使用传统的命名.
更新
要将环境变量传递给 maven 以进行集成测试,请使用以下内容:
- 确保您从 eclipse 市场安装了 M2E(在菜单 > 帮助 > eclipse 市场中找到)
- 右键单击您的项目 >运行方式... >4 Maven 构建...PS:之后您可以在绿色箭头下拉菜单下方的顶部和运行配置设置中找到您的运行配置,如果您以后需要重新运行测试
- 使用
verify -Dkey=val
内联(对于 Maven 目标命令)或在底部变量部分配置 Maven 环境参数.都适用于单元和集成测试.环境通常不适用于测试阶段.(如果你没有 JDK 作为运行器,你会得到一个错误.如果需要,请按照这篇文章修复它:希望有帮助.如果需要,您也可以更改 pom.xml 配置文件,但我不建议这样做.
I'm working on multi-module maven project using Spring Boot 2.4.0. I have written integration tests for a module. The test class looks similar to this.
@RunWith(SpringRunner.class) @SpringBootTest(classes = SpringApplicationClassWithMainMethod.class) public class XYZServiceIT { @Test public void test1() {...} @Test public void test2() {...} }
To run the SpringApplicationClassWithMainMethod.class i.e., in order to load the Application context I need few environment variables that I set in 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.
Trial-1: I have tried using @TestPropertySource(properties = {"key1=val1", "key2=val2"}) annotation, that didn't work.
Trial-2: I have also tried the static block to set environment variables which didnt work.
Trial-3: I also tried using the @ContextConfiguration with a ApplicationContextInitializer class, that didnt work as well.
All these attempts to build the project using maven only lead to a
IllegalState Failed to load ApplicationContext
error for the above test 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. You can change that if needed in your maven pom or simply stick with the conventional naming.UPDATE
To pass environment variables to maven for your integration test use the following:
- make sure you installed M2E from eclipse marketplace (found in menu > help > eclipse marketplace)
- 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
- configure maven environment parameters either inline (for the maven goal command) with
verify -Dkey=val
or in the bottom variable section. 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?)
I hope that helps. You can also change pom.xml profiles if needed but i wouldn't recommend that.
这篇关于如何在集成测试运行之前加载环境变量以加载应用程序上下文的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!