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

  • Integrate Cucumber in Playwright With Java
  • Test Automation: Maven Profiles and Parallelization in Azure Pipelines Using IaaS
  • DGS GraphQL and Spring Boot
  • Comprehensive Guide to Property-Based Testing in Go: Principles and Implementation

Trending

  • My LLM Journey as a Software Engineer Exploring a New Domain
  • Scalable, Resilient Data Orchestration: The Power of Intelligent Systems
  • Medallion Architecture: Efficient Batch and Stream Processing Data Pipelines With Azure Databricks and Delta Lake
  • How AI Agents Are Transforming Enterprise Automation Architecture
  1. DZone
  2. Coding
  3. Java
  4. Sharing Test Classes Between Multiple Modules in a Multi-module Maven Project

Sharing Test Classes Between Multiple Modules in a Multi-module Maven Project

Using Maven to effectively test software when test classes are not already packaged by Maven.

By 
Dave Turner user avatar
Dave Turner
·
Apr. 28, 16 · Tutorial
Likes (6)
Comment
Save
Tweet
Share
55.1K Views

Join the DZone community and get the full member experience.

Join For Free

Scenario

Project A has a class generated in its test branch. Project B (or C, D, etc..) wants to utilize the class generated in Project A’s test scope.

The Problem

Typically classes in the test branch are not packaged by Maven, therefore, Project B has no means to find the class/resource in Project A.

Solution

Edit the package phase of the jar plugin in Project A so a jar file containing the compiled test classes can be made available to other modules/projects.

Addition to the build section in Project A:

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-jar-plugin</artifactId>
  <version>2.6</version>
  <executions>
    <execution>
      <id>Jar Package</id>
      <phase>package</phase>
      <goals>
        <goal>jar</goal>
      </goals>
    </execution>
  <execution>
  <id>Jar Tests Package</id>
  <phase>package</phase>
  <goals>
  <goal>test-jar</goal>
  </goals>
  </execution>
  </executions>
</plugin>

In this example, two jars are created. One will be the typical packaged product associated with the “jar” goal of the “package” phase (a jar file containing compiled source under “src/main/java”).  A second jar is constructed containing the compiled source under “src/test/java” this is associated with the “test-jar” goal of the p”package” phase.

Files Generated

  • ExampleJar-0.0.1-SNAPSHOT.jar

  • ExampleJar-0.0.1-SNAPSHOT-tests.jar

Project B will contain 2 references to the ExampleJar dependency. One for the compilation process and another for the “test” scope of Project B.

Note: if the only requirement is for test dependencies than the dependency that is under the default scope wouldn’t be needed.

Example of how to reference the “test-jar” in Project B:

<dependency>
  <groupId>com.example</groupId>
  <artifactId>ExampleJar</artifactId>
  <version>${project.parent.version}</version>
</dependency>

<dependency>
  <groupId>com.example</groupId>
  <artifactId>ExampleJar</artifactId>
  <version>${project.parent.version}</version>
  <type>test-jar</type>
  <scope>test</scope>
</dependency>

Additional Points

Generally it’s not a good idea for test source bases to have dependencies. This scenario came to me while converting projects to Maven and mocks being shared amongst different projects.

Another way to solve this problem would be to place mocks classes and other test resources into a compiled dependency that is available to all modules.

Testing Apache Maven

Opinions expressed by DZone contributors are their own.

Related

  • Integrate Cucumber in Playwright With Java
  • Test Automation: Maven Profiles and Parallelization in Azure Pipelines Using IaaS
  • DGS GraphQL and Spring Boot
  • Comprehensive Guide to Property-Based Testing in Go: Principles and Implementation

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!