Normally, integration testing for web application involves creating a war and deploying it to web container and starting the container prior to running integration tests.
What happens when application is not deployed and container has not yet started while maven build ? Usually we face this problem while continuous integration build (CI build). I had similar issue in running my integration test cases when Jenkins CI build runs.
The Maven Build Lifecycle includes the "integration-test" phase for running integration tests, which are run separately from the unit tests run during the "test" phase. It runs after "package", so if you run "mvn verify", "mvn install", or "mvn deploy", integration tests will be run along the way. This will cause problem while building application if application is not deployed to container.
Use the Maven Jetty Plugin to start an instance of a server prior to running your integration tests. Jetty provides an HTTP server, HTTP client, and javax.servlet container. Jetty can be started in embedded mode and this is one of its main feature.
If you're using maven project, you can add below maven jetty plugin in project's pom file.
<plugin>
<groupId>org.mortbay.jetty</groupId>
<artifactId>maven-jetty-plugin</artifactId>
<version>6.1.26</version>
<configuration>
<scanIntervalSeconds>10</scanIntervalSeconds>
<stopKey>stop</stopKey>
<stopPort>9999</stopPort>
</configuration>
<executions>
<execution>
<id>start-jetty</id>
<phase>pre-integration-test</phase>
<goals>
<goal>run</goal>
</goals>
<configuration>
<scanIntervalSeconds>0</scanIntervalSeconds>
<daemon>true</daemon>
</configuration>
</execution>
<execution>
<id>stop-jetty</id>
<phase>post-integration-test</phase>
<goals>
<goal>stop</goal>
</goals>
</execution>
</executions>
</plugin>
How to start/stop jetty
In order to start the jetty in embedded mode before running integration test cases, bind "run" goal with "pre-integration-test" phase and bind "stop" goal with "post-integration-test" phase of maven build life cycle.
Note: You shouldn't run integration test case during test phase. To avoid this, you need to tell surfire/failsafe plugin about pattern of your test cases. This will be discussed in detail in some other post.
Start jetty from command line
mvn jetty:run
It is extremely convenient to leave the plugin running because it can be configured to periodically scan (based on plugin configuration <scanIntervalSeconds>) for changes and automatically redeploy the webapp. This makes development cycle much more faster and eliminates building and deployment of project.
Jetty can be started in embedded mode from your regular main() method and it just runs within the context of your application.
Stop jetty from command line
mvn jetty:stop

No comments:
Post a Comment