【问题标题】:End to end test across multi Spring Boot applications跨多个 Spring Boot 应用程序的端到端测试
【发布时间】:2016-04-23 20:43:21
【问题描述】:

目前在我们的项目中,我们正在使用 Spring Integration 来集成许多服务和一些协议相关的端点。

该项目是一个多 Spring Boot 应用程序,生产环境中会部署多个可执行 jar。

问题是:

  1. 如何运行需要跨其中一些应用程序运行的端到端测试,我必须手动运行一个一个?在非 Spring-Boot 应用之前,我可以使用 Maven tomcat7 插件来完成这项工作(将战争部署到嵌入式 tomcat 并在 pre-integration-test 阶段运行),现在如何开始在我运行测试之前启动所有相关的应用程序。假设我现在不使用 Docker/Vagrant。

    在 stackoverflow 上发现了类似的问题,End to end integration test for multiple spring boot applications under Maven

    如何自动运行端到端测试?

  2. 在 Spring 集成测试中,有时我必须模拟一个 http 端点,所以我在测试包中写了一个简单的 Controller 来归档这个目的,但我想在不同的端口上运行它,这使得它更像是一种外部资源。为此,如何在测试中同时在不同的端口上运行不同的@SpringBootApplicaiton类?

我正在使用最新的 Maven、Java 8、Spring Boot 1.3.1.RELEASE。

【问题讨论】:

  • 第二个问题,我发现Spring提供了一个MockRestServiceServer,可以mock目标远程REST API,在测试中实现这个目的。

标签: spring-boot spring-integration spring-test end-to-end


【解决方案1】:

实际上,Spring Boot 带有 embedded Servlet Container 支持。其中之一就是Tomcat。 org.springframework.boot:spring-boot-starter-web 默认开启。

使用org.springframework.boot:spring-boot-starter-test 及其@SpringApplicationConfiguration@WebIntegrationTest,您可以实现您的要求,即使是随机的port

更多信息请参考 Spring Boot Reference Manual

要更改端口,您可以将环境属性作为冒号或等号分隔的名称-值对添加到 @WebIntegrationTest,例如@WebIntegrationTest("server.port:9000")。此外,您可以将 server.port 和 management.port 属性设置为 0,以便使用随机端口运行集成测试。

您的@SpringBootApplicaiton 将部署到该嵌入式Tomcat,您的测试可以访问运行的服务/控制器。

注意:您的 Spring Boot 应用程序是否具有 Spring 集成功能并不重要。行为是相同的:嵌入式 Servlet 容器和针对 @Value("${local.server.port}") 的集成测试。

【讨论】:

  • 我很了解 Spring Boot 应用程序中的 @IntegrationTest@WebIntegrationTest。也许我的表达让你感到困惑,我不是英国人。我需要的是在启动几个 Spring Boot 应用程序之前运行我的独立端到端测试(我的问题中的案例 1)。
猜你喜欢
  • 2016-02-25
  • 1970-01-01
  • 2017-11-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-03-17
相关资源
最近更新 更多