gpt4 book ai didi

junit - 集成测试基于 Spring Boot 的微服务

转载 作者:行者123 更新时间:2023-12-04 04:16:51 26 4
gpt4 key购买 nike

我已经阅读了许多关于使用 Spring Boot 和 RESTful 服务的指南,其中许多包含有关运行单元测试的信息,最着名的是“使用 Spring Boot 构建应用程序”。但是,我还没有看到任何示例说明如何对使用/依赖于其他 Spring Boot 应用程序的 Spring Boot 应用程序进行单元测试,这在云微服务架构中很常见。因此,例如,我们有以下 Spring Boot 服务:

服务中介,
适配器 1,
适配器 2

ServiceMediator 根据输入调用 Adapter1 或 Adapter2。

在 Spring JUnit 测试中启动和测试 ServiceMediator 之前,有没有办法启动 Spring Boot 服务 Adapter1 和 Adapter2?

最佳答案

process-exec-maven-plugin 可能会有所帮助,因为它允许在 pre-integration-test 阶段启动多个 java 进程(作为标准 Spring Boot 应用程序),并且它会在 post-integration-test post-integration-test 阶段自动关闭它们。 _0x104

注:集成测试应在集成测试阶段运行的 Maven的故障保护,插件 Spring 启动了Maven插件 see进行配置。
然后运行我们的集成测试 验证 或更高版本的 maven Lifecycle 应该是目标,因为 集成测试 阶段实际上位于 包和 50x104567919 包之间的 _407910 _407070x701070x10705700x10705702010000000000000000000000000000000000007

以下 maven (pom.xml) 配置对我有用:

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>1.3.5.RELEASE</version>
<executions>
<execution>
<id>pre-integration-test</id>
<goals>
<goal>start</goal>
</goals>
<configuration>
<skip>${integration-tests.skip}</skip>
</configuration>
</execution>
<execution>
<id>post-integration-test</id>
<goals>
<goal>stop</goal>
</goals>
<configuration>
<skip>${integration-tests.skip}</skip>
</configuration>
</execution>
</executions>
</plugin>

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<version>2.19.1</version>
<configuration>
<skip>${integration-tests.skip}</skip>
<includes>
<include>**/*IT.java</include>
</includes>
</configuration>
<executions>
<execution>
<goals>
<goal>integration-test</goal>
<goal>verify</goal>
</goals>
</execution>
</executions>
</plugin>

<plugin>
<groupId>com.bazaarvoice.maven.plugins</groupId>
<artifactId>process-exec-maven-plugin</artifactId>
<version>0.7</version>
<executions>
<execution>
<id>switchboard-process</id>
<phase>pre-integration-test</phase>
<goals>
<goal>start</goal>
</goals>
<configuration>
<name>accounts-service</name>
<workingDir>/../../micro-service</workingDir>
<waitForInterrupt>false</waitForInterrupt>
<arguments>
<argument>java</argument>
<argument>-jar</argument>
<argument>${basedir}/../micro-service/target/micro-service-${project.version}-exec.jar</argument>
</arguments>
</configuration>
</execution>
<!--Stop all processes in reverse order-->
<execution>
<id>stop-all</id>
<phase>post-integration-test</phase>
<goals>
<goal>stop-all</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>

test.java 文件夹中有一个集成测试类( WebServerIT ):
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;

@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = WebServerApp.class)
@WebIntegrationTest("server.port:0")
public class WebServerIT {

@Autowired
private WebApplicationContext webServerAppContext;

private MockMvc webServerMockMvc;

@Before
public void setUp() {
System.out.println("the test is set up");
webServerMockMvc = MockMvcBuilders.webAppContextSetup(webServerAppContext).build();
}

/**
* This test calls the WebServer's endpoint (/accounts/123456789) which in turn calls the micro-service rest api
* which is started using the process-exec-maven-plugin, otherwise the test would fail.
*/
@Test
public void testWebServerInteractionWithMicroService() throws Exception {
this.webServerMockMvc.perform(get("/accounts/123456789"))
.andExpect(status().isOk());
}
}

关于junit - 集成测试基于 Spring Boot 的微服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29704842/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com