I currently develop a product which expose a a REST API and that will eventually be hosted on the cloud.
我目前开发的产品公开了一个REST API,最终将托管在云上。
My technology stack is spring (boot, mvc, data, test, etc.) on top of maven. I have integration tests to test from my API connected to a test database.
我的技术堆栈是maven上的spring(boot,mvc,data,test等)。我有集成测试来测试连接到测试数据库的API。
To better test my product in a same environment as it will run in production, I would like to use a container to perform my integration testing. My goal would be to follow this continuous delivery workflow :
为了更好地在与生产相同的环境中测试我的产品,我想使用容器来执行我的集成测试。我的目标是遵循这种持续交付工作流程:
- compile
- run unit tests
- build the application (jar) and deploy to a central repository
- create a docker container using this archive
- start the container (using spring boot)
- run integration tests against the running container
- run performance tests
- if everything is fine, deploy this container to a central repository
- deploy this same container to prod (just using different command line arguments).
运行单元测试
构建应用程序(jar)并部署到*存储库
使用此存档创建一个docker容器
启动容器(使用弹簧靴)
对正在运行的容器运行集成测试
运行性能测试
如果一切正常,请将此容器部署到*存储库
将此相同的容器部署到prod(只使用不同的命令行参数)。
The upside of this approach is having the same container form integration test phase to production, which seems ideal, wouldn't it?
这种方法的优点是具有相同的容器形式集成测试阶段到生产,这似乎是理想的,不是吗?
However I don't know how to do that using spring mvc tests that resides in my source package. How could I use mockmvc to do such thing? How can it be flexible enough to run integration tests in development also?
但是我不知道如何使用驻留在我的源包中的spring mvc测试。我怎么能用mockmvc做这样的事情?如何灵活地在开发中运行集成测试呢?
Has anyone tried such approach? Do I miss something here?
有人试过这种方法吗?我在这里想念一下吗?
Thanks in advance
提前致谢
2 个解决方案
#1
The Spring MVC Test Framework (i.e., MockMvc
) can not be used to test a Spring web application deployed in a Servlet container.
Spring MVC测试框架(即MockMvc)不能用于测试部署在Servlet容器中的Spring Web应用程序。
On the contrary, the primary goal of the Spring MVC Test Framework is to provide first-class "support for testing client and server-side Spring MVC code through a fluent API." Furthermore, it "uses the DispatcherServlet
to process requests thus approximating full integration tests without requiring a running Servlet container."
相反,Spring MVC测试框架的主要目标是提供一流的“支持,通过流畅的API测试客户端和服务器端的Spring MVC代码”。此外,它“使用DispatcherServlet处理请求,从而近似完整集成测试,而无需运行的Servlet容器。”
The quoted text above comes straight from the Spring Framework reference manual.
上面引用的文字直接来自Spring Framework参考手册。
In summary, the Spring MVC Test Framework can only be used for out-of-container integration tests. If you wish to test your Spring-based web application deployed in a Servlet container, you will need to use other frameworks such as HtmlUnit, HttpUnit, Selenium, etc.
总之,Spring MVC测试框架只能用于容器外集成测试。如果您希望测试部署在Servlet容器中的基于Spring的Web应用程序,则需要使用其他框架,例如HtmlUnit,HttpUnit,Selenium等。
Regards,
Sam
#2
I have been working on demo code that covers most of the bullets you pointed out, except for the last 3. And I just created a blog post: Integration Testing using Spring Boot, Postgres and Docker which references a couple of bitbucket repos for the Postgres DB Docker images (available images in Docker hub too) and a Spring Boot test demo.
我一直在研究涵盖你指出的大部分子弹的演示代码,除了最后的3个。我刚刚创建了一篇博文:使用Spring Boot,Postgres和Docker进行集成测试,它引用了Postgres的几个bitbucket回购DB Docker图像(Docker hub中的可用图像)和Spring Boot测试演示。
It basically uses a custom implementation of Spring's TestExecutionListener to hook into the test method life cycle to manage pulling Docker image, starting / stopping Docker containers, before and after test executes. It could use the same container for all integration tests or could start a new container for each test, it's configurable.
它基本上使用Spring的TestExecutionListener的自定义实现来挂钩测试方法生命周期,以便在测试执行之前和之后管理拉动Docker镜像,启动/停止Docker容器。它可以使用相同的容器进行所有集成测试,也可以为每个测试启动一个新容器,它是可配置的。
Best, Orlando
#1
The Spring MVC Test Framework (i.e., MockMvc
) can not be used to test a Spring web application deployed in a Servlet container.
Spring MVC测试框架(即MockMvc)不能用于测试部署在Servlet容器中的Spring Web应用程序。
On the contrary, the primary goal of the Spring MVC Test Framework is to provide first-class "support for testing client and server-side Spring MVC code through a fluent API." Furthermore, it "uses the DispatcherServlet
to process requests thus approximating full integration tests without requiring a running Servlet container."
相反,Spring MVC测试框架的主要目标是提供一流的“支持,通过流畅的API测试客户端和服务器端的Spring MVC代码”。此外,它“使用DispatcherServlet处理请求,从而近似完整集成测试,而无需运行的Servlet容器。”
The quoted text above comes straight from the Spring Framework reference manual.
上面引用的文字直接来自Spring Framework参考手册。
In summary, the Spring MVC Test Framework can only be used for out-of-container integration tests. If you wish to test your Spring-based web application deployed in a Servlet container, you will need to use other frameworks such as HtmlUnit, HttpUnit, Selenium, etc.
总之,Spring MVC测试框架只能用于容器外集成测试。如果您希望测试部署在Servlet容器中的基于Spring的Web应用程序,则需要使用其他框架,例如HtmlUnit,HttpUnit,Selenium等。
Regards,
Sam
#2
I have been working on demo code that covers most of the bullets you pointed out, except for the last 3. And I just created a blog post: Integration Testing using Spring Boot, Postgres and Docker which references a couple of bitbucket repos for the Postgres DB Docker images (available images in Docker hub too) and a Spring Boot test demo.
我一直在研究涵盖你指出的大部分子弹的演示代码,除了最后的3个。我刚刚创建了一篇博文:使用Spring Boot,Postgres和Docker进行集成测试,它引用了Postgres的几个bitbucket回购DB Docker图像(Docker hub中的可用图像)和Spring Boot测试演示。
It basically uses a custom implementation of Spring's TestExecutionListener to hook into the test method life cycle to manage pulling Docker image, starting / stopping Docker containers, before and after test executes. It could use the same container for all integration tests or could start a new container for each test, it's configurable.
它基本上使用Spring的TestExecutionListener的自定义实现来挂钩测试方法生命周期,以便在测试执行之前和之后管理拉动Docker镜像,启动/停止Docker容器。它可以使用相同的容器进行所有集成测试,也可以为每个测试启动一个新容器,它是可配置的。
Best, Orlando