Last weeks I had to deal with implementing several rest apis. Some with Play 2, some with Spring MVC. Due only Play supports build-in mechanism for integration tests I had to integrate support for them in every project where I used Spring MVC. Those are typically Maven builds. Maven does not support separate integration tests with a running Tomcat (or any other webcontainer) by default. Hence you have to get your hands dirty.
The goal is to execute all JUnit tests that are suffixed with IntegrationTest
, start a Tomcat before (I want to test a deployed rest api) and afterwards shutdown the running Tomcat.
For that you have to exclude all integration tests in the normal test phase of Maven.
1 2 3 4 5 6 7 8 9 10 |
|
Now you should add all integration tests to the Maven Failsafe Plugin and activate the execution of the plugin in your build.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
|
After this the only missing step is a working Tomcat integration in your build. In this example I’m using the Tomcat 7 Maven Plugin for this (the Tomcat 6 plugin should also work). With the start
and stop
goals you can control a Tomcat instance. Important is that you fork the process, otherwise your build would be blocked by the running Tomcat. You can do similiar things with the Jetty Maven Plugin.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
|
With this little setup you get full support for integration tests with Maven and Tomcat in your web applications, especially if you building rest apis. I hope that this Maven snippet helps you to improve your builds.
Update [11.02.2013] Changed suffix of include and exclude configurations. Thanks to a hint by Erich Eichinger in the comment section.