Thursday, August 18, 2011

Java Integration Tests

You have unit tests and integration tests, but how do you manage the two?  Can you easily partion them so that you only run unit tests at one time and integration tests at another?  Then have them both run before building your artifact?  Yea, maven makes it real easy.  You might already know about the surefire plugin, the plugin used to run unit tests.  It looks for classes ending or starting with Test (**/Test*.java, **/*, and **/* and runs them in the "test" phase.  But there is another plugin, failsafe, which is used to run integration tests and is run during the "integration-test" phase.  The failsafe plugin looks for classes ending or starting with IT (**/IT*.java, **/*, and **/* and runs those.

The failsafe plugin has four phases that it uses:
  1. pre-integration-test - the setup phase.  use it to start a webserver or configure data in a database.
  2. integration-test - runs the tests.
  3. post-integration-test - the teardown phase.  use it to stop your webserver or clean your database.
  4. verify - used to help intepret results of the tests.  if any tests failed, the build will exit.



Another approach that I have done on previous projects is to leave all my unit tests in their appropriate projects and package all of my integration tests into a separate project that I run every few hours everyday in an integration test environment.