Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best practices for integration tests with Maven?

I have a project which I am building with Maven which uses Hibernate (and Spring) to retrieve data from a database, etc.

My "tests" for the DAOs in my project extend Spring's AbstractTransactionalDataSourceSpringContextTests so that a DataSource can be wired into my class under test to be able to actually run the query/Hibernate logic, to fetch data, etc.

On several other projects I've used these types of test in concert with a HSQL database (either in-memory or pointed at a file) to be able to efficiently test the actual database querying logic without relying on an external database. This works great, since it avoids any external dependencies and the "state" of the database prior to running the tests (each of which are wrapped in a transaction which is rolled back) is well defined.

I'm curious though about the best way to organize these tests, which are really a loose flavor of integration tests, with Maven. It feels a bit dirty to keep these tests in src/test/java, but from what I've read there doesn't seem to be a consistent strategy or practice for organizing integration tests with Maven.

From what I've read so far, seems like I can use the Failsafe plugin (or a second instance of Surefire) and bind it to the integration-test phase, and that I can also bind custom start-up or shutdown logic (such as for starting/stopping the HSQL instance) to pre-integration-test or post-integration-test. But, is this really the best method?

So my question basically is - what is the generally accepted best practice on organizing this with Maven? I'm having trouble finding any sort of consistent answer in the documentation.

What I'd like is to:

  • Seperate unit tests from integration tests, so only unit tests are run during the test phase
  • The ability to bind custom startup/shutdown logic to pre-integration-test and post-integration-test
  • Have the reports from the integration-tests merged/presented with the unit test Surefire reports
like image 503
matt b Avatar asked Aug 04 '09 17:08

matt b


People also ask

How do I run an integration-test in Maven?

The simplest way to run integration tests is to use the Maven failsafe plugin. By default, the Maven surefire plugin executes unit tests during the test phase, while the failsafe plugin runs integration tests in the integration-test phase.

Does mvn install run integration tests?

The Maven build lifecycle now 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.

Can Maven be integrated with JUnit test framework?

Junit Framework can be integrated with Eclipse, Ant and Maven, but in this article we will be using Maven.


1 Answers

A very simple way of doing this is to use JUnit categories.

You can then easily run some tests during the test phase and another during the integration-test phase.

It takes minutes and requires only 3 steps.

  1. Define a marker interface
  2. Annotate the classes you wish to split
  3. Configure Maven plugins.

A full example is given here. https://stackoverflow.com/a/10381662/1365383

like image 84
John Dobie Avatar answered Oct 05 '22 21:10

John Dobie