Adam Bien's Weblog

Wednesday Jun 08, 2011

A Natural Separation of JUnit Tests in Maven (3)

Unit tests are fast, and integration tests are slow. Unit tests are also executed more frequently than integration tests. Tests ending with "*IT" are automatically executed by the failsafe plugin:


<dependency>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-failsafe-plugin</artifactId>
    <version>2.8.1</version>
    <scope>test</scope>
</dependency>

Unit tests ending with "*Test" are executed by the standard surefire plugin
Now you can execute all unit tests with mvn install (or test) and integration tests with mvn failsafe:integration-tests. You will get immediate feedback after the execution of unit tests. The integration tests will be executed afterwards (in a separate Jenkins job).

[All x-ray unit tests are separated this way in separate Jenkins jobs. See page 131 "Build and Deployment" and page 136 "Continuous Integration and QA" in Real World Java EE Night Hacks--Dissecting the Business Tier.]


Special Events: Java 8 with Java EE 7: "More Power with Less Code", 13th October, 2014 and Java EE 7: "Testing and Code Quality", 14th October, 2014

A book about rethinking Java EE Patterns

Comments:

Do you know of a failsafe-plugin for netbeans? The *IT Tests do not show up in the tests-tab.

Posted by 80.254.148.123 on June 08, 2011 at 06:01 PM CEST #

good

Posted by sandeep sadwal on June 08, 2011 at 09:25 PM CEST #

@80.254.148.123,

in NetBeans 7 (with Maven 3) you can just run the files named *IT as a JUnit test.

Furthermore you can add a new menu entry with:

Project -> Properties -> Actions -> Add Custom and enter into "Execution Goals" failsafe:integration-tests. NetBeans will even autocomplete the goal...

Enjoy Java EE with NetBeans!,

adam

Posted by Adam Bien on June 09, 2011 at 01:39 AM CEST #

this can be done using surefire and excludes includes elements

Posted by Lukasz on June 09, 2011 at 02:05 AM CEST #

@Lukasz,

yes, you could also re-configure surefire for that purpose, but:

1. Failsafe comes already with usable configuration - no additional work is needed. You only have to add the plugin configuration from above.
2. You can execute failsafe with a dependent Jenkins build. It will speed-up the main build.

thanks for your opinion!,

adam

Posted by Adam Bien on June 10, 2011 at 12:35 PM CEST #

The link above might be a might more complicated than you are after, but hopefully it might help

http://johndobie.blogspot.com/2011/06/seperating-maven-unit-integration-tests.html

Posted by John Dobie on June 29, 2011 at 07:36 PM CEST #

According to the documentation, the failsafe plugin is bound to the integration test phases of the build cycle. No need to invoke mvn failsafe:integration-tests just call mvn verify or am I wrong?

Moreover install phase occurs after the integration-test. This means integration tests will always be run if you invoke mvn install

Posted by Jean-Marc Borer on August 12, 2011 at 08:18 PM CEST #

Jean-Marc, if the documentation states that the failsafe plugin is bound to the integration-test lifecycle phase, and I couldn't locate such a claim, though my not finding it doesn't indicate very much, then the documentation appears to be wrong.

I am running and experimenting with Maven 3.0.3 and so far it looks as if no plugins are bound to the integration-test lifecycle phase by default.

I was able to bind the failsafe plugin to the integration-test phase, and once I do that I get the results that you describe, and I witness the behavior that Adam wrote about.

So yes, once bound, mvn <lifecycle phase>, where lifecycle phase is integration-test or greater will fire the surefire plugin and execute the unit tests when it hits the test lifecycle phase and then it will fire the failsafe plugin and execute what it identifies as the integration tests when it hits the integration-test lifecycle phase.

Out of the box with no additional configuration of the failsafe plugin, the plugin will execute any files ending in *IT.java in the standard test code tree. You can modify the location of the integration test code in the failsafe plugin's config section, but I wanted to describe what could be done with the smallest amount of pom customization.

Posted by Andy Glick on August 20, 2011 at 01:40 AM CEST #

Hi Adam,
Good solution, but I would define this rather as a plugin in the build element than as a dependency:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<version>2.8.1</version>
</plugin>

BTW, it's "mvn failsafe:integration-test", not "mvn failsafe:integration-tests".
(just being picky, I know)

Posted by Dirk Estievenart on January 04, 2012 at 06:41 PM CET #

Post a Comment:
  • HTML Syntax: NOT allowed
realworldpatterns.com
...the last 150 posts
...the last 10 comments
Links
License