3
votes

I currently develop a product which expose a a REST API and that will eventually be hosted on the cloud.

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.

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).

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?

Has anyone tried such approach? Do I miss something here?

Thanks in advance

2
I do something similar, though for ruby on rails, I am able to run an instance and have the test run outputting to stdout - that allows jenkins to grab the output. just a docker run image /bin/run_tests effectively. - Michael

2 Answers

1
votes

The Spring MVC Test Framework (i.e., MockMvc) can not be used to test a Spring web application deployed in a Servlet container.

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."

The quoted text above comes straight from the Spring Framework reference manual.

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.

Regards,

Sam

1
votes

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.

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.

Best, Orlando