简体   繁体   English

Spring 开机集成测试带环境变量

[英]Spring boot integration test with environment variables

I'm working on a multi-module maven project using Java 8, Spring Boot 2.4.0.我正在使用 Java 8、Spring Boot 2.4.0 开发多模块 maven 项目。 I want to test one of a module that is calling a 3rd party service.我想测试一个调用 3rd 方服务的模块。 I'm using wiremock to mock that 3rd party service call and have created a spring boot integration test class.我正在使用wiremock来模拟第3方服务调用,并创建了一个spring引导集成测试class。 My class is in the same package where my XYZService class is.我的 class 与我的 XYZService class 位于同一个 package 中。 My test is in src/test/... and looks like this.我的测试在 src/test/... 中,看起来像这样。

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

@Rule
public WireMockRule externalService = new WireMockRule();

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

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

When I run the maven build in my eclipse (clean + install).当我在我的 eclipse 中运行 maven 构建(清洁 + 安装)。 My build is not detecting the tests present at the maven-module where the Integration test is located.我的构建没有检测到集成测试所在的 maven-module 中存在的测试。 The other unit tests in the same module are also not being detected (Note: before adding the integration test class, the unit tests were working).同一模块中的其他单元测试也没有被检测到(注意:在添加集成测试 class 之前,单元测试正在工作)。 Maven says Tests ran: 0. The integration test is working fine when I Right click on the file and run as JUnit test(Junit 4). Maven 说测试运行:0。当我右键单击文件并以 JUnit 测试(Junit 4)运行时,集成测试工作正常。 Also, I have some environment variables that need to be set for running the SpringApplicationClassWithMainMethod.class that I'm setting within the configurations of that Integration test class in order to successfully load the Application context(I tried to load the environment variables through code and nothing from other stack-overflow posts worked).另外,我需要设置一些环境变量来运行 SpringApplicationClassWithMainMethod.class ,我在该集成测试 class 的配置中设置这些环境变量,以便成功加载应用程序上下文(我尝试通过代码和其他堆栈溢出帖子没有任何效果)。 One more thing to inform my maven only uses Maven surefire plugin for running tests.还有一件事要告诉我的 maven 只使用 Maven 肯定插件来运行测试。 I dont know if we need to have Maven fail safe plugin for my purpose(Is my test considered a Integration Test when I added the 2 annotations on top of the test class?).我不知道我们是否需要为我的目的使用 Maven 故障安全插件(当我在测试 class 顶部添加 2 个注释时,我的测试是否被视为集成测试?)。 Can someone please help me with any suggestions on how to build the parent project.有人可以帮助我就如何构建父项目提出任何建议。

Adding the maven-failsafe-plugin into my child pom worked.将 maven-failsafe-plugin 添加到我的孩子 pom 中有效。 In the parent pom I added the execution goals for maven-failsafe-plugin in the and in the child pom I inherited that plugin.在父 pom 中,我在继承该插件的子 pom 中添加了 maven-failsafe-plugin 的执行目标。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM