We assume that the application we want to run defaults to some standalone setup, e.g. by using something like an in-memory instance of hsqldb as persistence solution. It also has to be available via Maven, in the simple case by running a "mvn install" locally, the nicer solution involves running your own repository (e.g. using Artifactory).
Here is a sample POM for such a project he rest of the story is in the inline documentation:
=== POM for testing a war file coming out of a separate build. ===
- run Surefire plugin for tests, but in the integration-test phase
- use Cargo to start an embedded Servlet engine before the integration-tests, shut it down afterwards
- use tests written with JWebUnit with the HtmlUnit backend to do the actual testing work
This configuration can be run with "mvn integration-test".
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
<name>MyApp Test Suite</name>
<description>A set of functional tests for my web application</description>
<!-- The Cargo plugin manages the Servlet engine -->
<!-- start engine before tests -->
<!-- stop engine after tests -->
<!-- we use a Jetty 6 -->
<!-- don't let Jetty ask for the Ctrl-C to stop -->
<!-- the actual configuration for the webapp -->
<!-- pick some port likely to be free, it has be matched in the test definitions -->
<!-- what to deploy and how (grabbed from dependencies below) -->
<!-- configure the Surefire plugin to run integration tests instead of the
running in the normal test phase of the lifecycle -->
<!-- we use the HTML unit variant of JWebUnit for testing -->
<!-- we depend on our own app, so the deployment setup above can find it -->
Note that in this case we depend on a SNAPSHOT release of our own app. This means that you can nicely run this test in your continuous integration server, triggered by the successful build of your main application (as well as any change in the tests itself). If you use my favorite CI server Hudson, then you can even tell it to aggregate the test results onto the main project.
Having a POM like this you can launch straight into the JWebUnit Quickstart. A basic setup needs only the POM and one test case in
src/test/java. Of course you can use a different testing framework if you want to.
You can also switch the Servlet engine or deploy into a running one. The Cargo documentation is pretty decent, so I recommend looking at that. They certainly know more Servlet engines than I do.
If you use profiles to tweak Cargo in the right way, you should be able to run the same test suite against a proper test system using the same stack as your production environment. I haven't gone there yet, but I intend to. The setup used here is really intended for regular tests after each commit. By triggering them automatically on the build server the delay doesn't burden the developers, but they are still fast enough to give you confidence in what you are doing.