Sharing src/test classes between modules in a multi-module maven project
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
In a multi-module Maven project, effective code sharing between modules is crucial for maintaining a clean, DRY (Don't Repeat Yourself) codebase. While sharing code in the src/main directory is relatively straightforward through dependencies, sharing code from src/test presents unique challenges and requires specific strategies.
Understanding the Structure
Maven multi-module projects often have the following structure:
Goals of Sharing Test Code
- Avoid redundancy: Code reuse in test classes avoids redundancy across modules.
- Consistency: Ensures consistent testing standards and practices.
- Maintenance efficiency: Simplifies maintenance when alterations are necessary.
Approaches to Sharing Test Code
1. Using a Test Utility Module
The simplest approach is to create a separate module, such as shared-test-resources, specifically for shared test classes and utilities. This module will not produce deployable artifacts but instead function as a dependency for other modules' test scopes.
Key Implementations Steps:
- Configure the POM: Set up the
shared-test-resourcesmodule with its ownpom.xml. Make sure it is configured to create a JAR that can be used as a test dependency.
- Include the Test Dependency: Set up each module (e.g.,
module-a,module-b) to utilize theshared-test-resourcesas a test dependency.
2. Leveraging Test Classpath
The test classpath can also be leveraged to share resources by placing shared test classes in the src/main directory of the shared-test-resources module. This allows these resources to be accessed during the test phase without explicitly adding them as part of a test resources artifact.
3. Using Source Sets
Although a bit more complex, customizing the lifecycle to include custom source sets can also be employed to handle test resource sharing by configuring specific plugins to manage test resource packaging and dependencies.
Considerations
- Isolation: Shared test resources should usually be isolated from production code and kept in their relevant scope.
- Build Times: Ensure that sharing test resources does not inadvertently increase build times or complicate module interdependencies.
- Testing Framework Compatibility: Ensure compatibility with the testing frameworks being used in your modules.
Example Use Case
Suppose module-a and module-b both need access to a common DatabaseTestHelper class that simplifies database setups for testing. By placing this class in shared-test-resources, both modules can efficiently reuse the code:
Summary Table
| Aspect | Approach | Description |
| Basic structure | Test Utility Module | Create a module dedicated to shared test utilities. |
| Dependency setup | Configure scope and classifier | Define dependencies with test scope and attach proper classifiers. |
| Classpath sharing | Use src/main for tests | Place shared test artifacts in src/main for inclusion in the test phase. |
| Complex setups | Source Sets | Manipulate the Maven build lifecycle with custom source sets if necessary. |
| Advantages | Reuse, Consistency, Efficiency | Avoid redundancy and maintain coherent testing standards. |
By creating a well-structured sharing strategy, developers can significantly enhance the modularity and maintainability of multi-module Maven projects, especially concerning tests.
Related reading
- Short form for Java if statement
- Shortest way of checking if Double is NaN
- Should a static final Logger be declared in UPPER-CASE?
- Should I always use a parallel stream when possible?
- Should I avoid the use of set(Preferred|Maximum|Minimum) size methods in Java Swing?
- Should I declare Jackson's ObjectMapper as a static field?
- Should I instantiate instance variables on declaration or in the constructor?
- Should I learn about data structures and algorithms first or the programming language Java first?

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.