[英]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 以进行集成测试,请使用以下命令:
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 上运行? )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.