DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Join us tomorrow at 1 PM EST: "3-Step Approach to Comprehensive Runtime Application Security"
Save your seat
  1. DZone
  2. Testing, Deployment, and Maintenance
  3. Testing, Tools, and Frameworks
  4. Running Unit Tests and Integration Tests Separately With Maven Failsafe and TestNG

Running Unit Tests and Integration Tests Separately With Maven Failsafe and TestNG

Tomasz Dziurko user avatar by
Tomasz Dziurko
·
Jan. 24, 13 · Interview
Like (0)
Save
Tweet
Share
15.00K Views

Join the DZone community and get the full member experience.

Join For Free

recently for my new pet project i decided that i would like to have some tests executed during standard mvn test and some other ones only during different phase, let’s call it integration phase. i googled and googled and nothing seemed to work, so after struggling with making my setup work i’ve decided to write down my findings how i was able to configure testng with maven to run integration and unit tests separately.

basic (not working) setup

for integration testing there is a maven failsafe plugin that is supposed to do what we want out of the box. unfortunately, things are not as easy and straightforward as we might expect.

let’s look at basic setup that should work:

we have two test classes, one with it postfix that will indicate that this is our integration test:

import org.testng.annotations.test;
 
import static org.fest.assertions.assertions.assertthat;
 
public class exampleunittest {
 
    @test
    public void shouldpass() {
        assertthat(false).isfalse();
    }
}

and

import org.testng.annotations.test;
 
import static org.fest.assertions.assertions.assertthat;
 
public class statustestit {
 
    @test
    public void shouldfail() {
        assertthat("aaa").isequalto("");
    }
}

and now let’s declare plugin mentioned above in our pom.xml:

<project>
     
    <!-- ... -->
    <build>
        <plugins>
            <!-- ... -->
            <plugin>
                <groupid>org.apache.maven.plugins</groupid>
                <artifactid>maven-surefire-plugin</artifactid>
                <version>2.9</version>
            </plugin>
 
            <plugin>
                <groupid>org.apache.maven.plugins</groupid>
                <artifactid>maven-failsafe-plugin</artifactid>
                <version>2.13</version>
                <configuration>
                    <includes>
                        <include>**/*it.java</include>
                    </includes>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

after that we should see two things. mvn test should pass as we have a one green test in exampleunittest class, but mvn failsafe:integration-test should fail with statustestit red. but as i said before, this does not work. mvn test looks as expected, but second maven execution is passing as well showing that no test were run. plugin seems to omit our completely valid test…

small fix doing its job

after testing different approaches i found out that all we need to make this setup work is to add plugin execution to an integration-test phase of maven life cycle. so tiny change, but now our integration tests are executed only when we call mvn integration-test .

<plugin>
    <groupid>org.apache.maven.plugins</groupid>
    <artifactid>maven-failsafe-plugin</artifactid>
    <version>2.13</version>
    <configuration>
        <includes>
            <include>**/*it.java</include>
        </includes>
    </configuration>
    <executions>
        <execution>
            <id>failsafe-integration-tests</id>
            <phase>integration-test</phase>
            <goals>
                <goal>integration-test</goal>
            </goals>
        </execution>
    </executions>
</plugin>

and that’s all, we could execute integration tests only when we really want to. of course there are some things to remember when adopting setup described above:

  • our unit tests must have test postfix, if you use something else, we have to configure surefire plugin using include
  • during integration test unit tests are executed too. this is a small flaw in this setup, but exclude **/*test.java seem not to work :(

source code of this example setup is available on github





unit test Integration Apache Maven TestNG integration test

Published at DZone with permission of Tomasz Dziurko, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Spring Boot Docker Best Practices
  • Using QuestDB to Collect Infrastructure Metrics
  • Exploring the Benefits of Cloud Computing: From IaaS, PaaS, SaaS to Google Cloud, AWS, and Microsoft
  • Beginners’ Guide to Run a Linux Server Securely

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: