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
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

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

Last call! Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • The Most Popular Technologies for Java Microservices Right Now
  • Maven Plugin Testing - in a Modern Way - Part I
  • Comparison of Various AI Code Generation Tools
  • Integrate Cucumber in Playwright With Java

Trending

  • Infrastructure as Code (IaC) Beyond the Basics
  • Endpoint Security Controls: Designing a Secure Endpoint Architecture, Part 1
  • Transforming AI-Driven Data Analytics with DeepSeek: A New Era of Intelligent Insights
  • How AI Agents Are Transforming Enterprise Automation Architecture
  1. DZone
  2. Testing, Deployment, and Maintenance
  3. Deployment
  4. Integration Tests With Maven

Integration Tests With Maven

Integration tests with Maven have different requirements from simple unit tests. This article covers some of these different configurations.

By 
Emmanouil Gkatziouras user avatar
Emmanouil Gkatziouras
DZone Core CORE ·
Jan. 03, 18 · Tutorial
Likes (18)
Comment
Save
Tweet
Share
42.1K Views

Join the DZone community and get the full member experience.

Join For Free

Implementing unit tests with Maven is something ordinary, and most of us are familiar with the project structure and where the unit tests reside. However, integration tests are a different case and most of the time they have pretty different requirements.

For example, it is OK to have your unit tests operate on an h2 in-memory database, but when it comes to integration tests and complex queries, it is best for them to be executed against an actual relational database like the one used in production.

Considering the above example, chances are that our integration tests might have different configurations and dependencies, so our goals for our integration tests is to keep them separate from other tests and to execute them separately.

Regarding our Maven structure within the source directory, we have the main "folder" and test "folder." In the same fashion, we will add an extra directory called "it." The "it" directory will contain a Java directory for our Java sources and a resources directory. We'll use the same structure for the test directory.

Then we should give instructions to Maven on building our code and adding it to our test sources:

<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>build-helper-maven-plugin</artifactId>
    <version>1.5</version>
    <executions>
        <execution>
            <id>add-test-source</id>
            <phase>process-resources</phase>
            <goals>
                <goal>add-test-source</goal>
            </goals>
            <configuration>
                <sources>
                    <source>src/it/java</source>
                </sources>
            </configuration>
        </execution>
    </executions>
</plugin>

The next step is to instruct Maven to run our unit tests. To do so, we will use the Maven failsafe plugin. The Failsafe Plugin is designed to run integration tests, while the Surefire Plugin is designed to run unit tests. The name (failsafe) was chosen both because it is a synonym of surefire, and because it implies that when it fails, it does so in a safe way.

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-failsafe-plugin</artifactId>
                <version>2.15</version>
                <executions>
                    <execution>
                        <id>integration-test</id>
                        <goals>
                            <goal>integration-test</goal>
                        </goals>
                    </execution>
                    <execution>
                        <id>verify</id>
                        <goals>
                            <goal>verify</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>

Then, to execute the integration tests, we should issue:

mvn verify

As expected, our integration tests run separately.

To sum up, we want to have integration tests separated from our unit tests. Also, we want them to run separately. This can be achieved by utilizing the Maven failsafe plugin.
You can find an example project on GitHub.

unit test Integration Apache Maven

Published at DZone with permission of Emmanouil Gkatziouras, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • The Most Popular Technologies for Java Microservices Right Now
  • Maven Plugin Testing - in a Modern Way - Part I
  • Comparison of Various AI Code Generation Tools
  • Integrate Cucumber in Playwright With Java

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

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

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!