Marcin Zajaczkowski

About Marcin Zajaczkowski

Marcin is an experienced architect who specializes in creating high quality software. Being under the impression of the Agile methodologies and the Software Craftsmanship movement, he believes in the value of good, testable and maintainable code. He aims to forge good software that makes the client delighted and the team proud of how the code itself looks.

TestNG and Maven configuration guide

To be useful automatic tests should run very fast. Otherwise will not be run often during development or even will be ignored in the default configuration on the developer workstations. The simplest rule it to write only small unit tests which test given class having the neighborhood mocked. Nevertheless sometimes it is useful/required to test something in IoC container context (Spring, CDI, Guice) or using embedded database (H2, HyperSQL, Derby). Unfortunately even a few tests of that kind can increase significantly overall test execution time. I had that situation in one of my projects and to prevent using skipTests flag I developed a solution using groups from TestNG and Maven Surefire plugin.

Tests were divided into three groups:

  • very fast real unit tests (all by default) – should be run very often during development (from IDE or by mvn test, mvn package)
  • slower integration but self-sufficient tests (setting up Spring context and/or using embedded H2 database) – should be run at least before commit/push or while working on given part (from IDE or by mvn integration-test, mvn install)
  • real integration tests (requiring access to remote servers – e.g. testing web services or REST) – should be run daily by CI server or by developers working on the integration (mvn install, mvn integration-test with additional profile enabled)

To achieve that given tests (or test classes) have to be marked as “self-integration” or “integration” (at method or class level):

@Test(groups = "self-integration")
public void shouldInitializeChainedAppInfoProperly() {
@Test(groups = "integration")
public class FancyWebServiceIntegrationTest {

Maven Surefire plugin should be configured to exclude “self-integration” and “integration” test groups from default execution and add “self-integration” to “integration-test phase:

<build>

    <plugins>

        (...)

        <plugin>

            <groupId>org.apache.maven.plugins</groupId>

            <artifactId>maven-surefire-plugin</artifactId>

            <version>${ver.surefire-plugin}</version>

            <executions>

                <execution>

                    <id>default-test</id> <!-- to override default configuration - in fact: unit tests -->

                    <configuration>

                        <excludedGroups>self-integration,integration</excludedGroups>

                    </configuration>

                </execution>

                <execution>

                    <id>self-integration</id>

                    <phase>integration-test</phase>

                    <goals>

                        <goal>test</goal>

                    </goals>

                    <configuration>

                        <groups>self-integration</groups>

                        <reportsDirectory>target/self-integration-surefire-reports/</reportsDirectory>

                    </configuration>

                </execution>

            </executions>

        </plugin>

    </plugins>

</build>

In addition (if needed) the separate separate profile with “integration” test group configured in “integration-test” phase could be created.

<profiles>

    (...)

    <profile>

        <id>integration</id>

        <build>

            <plugins>

                <plugin>

                    <groupId>org.apache.maven.plugins</groupId>

                    <artifactId>maven-surefire-plugin</artifactId>

                    <version>${ver.surefire-plugin}</version>

                    <executions>

                        <execution>

                            <id>integration</id>

                            <phase>integration-test</phase>

                            <goals>

                                <goal>test</goal>

                            </goals>

                            <configuration>

                                <groups>integration</groups>

                                <reportsDirectory>target/integration-surefire-reports/</reportsDirectory>

                            </configuration>

                        </execution>

                    </executions>

                </plugin>

            </plugins>

        </build>

    </profile>

</profiles>

Working example can be find in AppInfo‘s artificial branch (pom.xml and sample test class). It’s easy to adopt it to your needs.

All three test groups have separate reports format to not override each other. As the extension it could probably possible to merge they into one aggregated test report.

Reference: Run fast unit tests often, slow integration rarely – TestNG and Maven configuration guide from our JCG partner Marcin Zajaczkowski at the Solid Soft blog.

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you two of our best selling eBooks for FREE!

JPA Mini Book

Learn how to leverage the power of JPA in order to create robust and flexible Java applications. With this Mini Book, you will get introduced to JPA and smoothly transition to more advanced concepts.

JVM Troubleshooting Guide

The Java virtual machine is really the foundation of any Java EE platform. Learn how to master it with this advanced guide!

Given email address is already subscribed, thank you!
Oops. Something went wrong. Please try again later.
Please provide a valid email address.
Thank you, your sign-up request was successful! Please check your e-mail inbox.
Please complete the CAPTCHA.
Please fill in the required fields.

Leave a Reply


eight − = 3



Java Code Geeks and all content copyright © 2010-2014, Exelixis Media Ltd | Terms of Use | Privacy Policy | Contact
All trademarks and registered trademarks appearing on Java Code Geeks are the property of their respective owners.
Java is a trademark or registered trademark of Oracle Corporation in the United States and other countries.
Java Code Geeks is not connected to Oracle Corporation and is not sponsored by Oracle Corporation.
Do you want to know how to develop your skillset and become a ...
Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you two of our best selling eBooks for FREE!

Get ready to Rock!
You can download the complementary eBooks using the links below:
Close