In my previous post, I wrote about splitting unit and integration tests to run in different Maven profiles. In this post, I’ll take this one step further by showing how I split the source code for different types of tests in my blog application. Although splitting the tests wasn’t critical due to the application's complexity, I saw it as an important step towards improving the overall structure of my code. Separating concerns in this way helps keep the codebase clean and well-organized.

Moving Test Classes

I decided to move the test classes into their own source directories but still kept them under the src/test directory. This allows all tests to reside in one place while maintaining clarity and organization. This structure also keeps my testing framework consistent, as all tests continue to run with JUnit.

Here’s the structure I implemented:

.
├── pom.xml
└── src
    ├── main
    │   ├── java
    │   │   └── ...
    │   └── resources
    │       └── application.properties
    │
    └── test
        ├── integration
        │   ├── java
        │   │   └── com
        │   │       └── domain
        │   │           └── app
        │   │               ├── ApplicationIT.java
        │   │               └── ModularityIT.java
        │   └── resources
        │       ├── application.properties
        │       └── test.sql
        └── unit
            └── java
                └── com
                    └── domain
                        └── app
                            ├── module1
                            │   └── Module1ServiceTest.java
                            └── module2
                                └── Module2ServiceTest.java

Adding Test Source Directories into Maven

By default, Maven expects test sources to be located under the src/test directory. Since I changed this structure, I needed to add the appropriate configurations to ensure Maven includes these new directories in the build.

To achieve this, I used the MojoHaus build-helper-maven-plugin:

<plugin>
  <groupId>org.codehaus.mojo</groupId>
  <artifactId>build-helper-maven-plugin</artifactId>
  <executions>
    <execution>
      <id>add-test-source</id>
      <phase>generate-test-sources</phase>
      <goals>
        <goal>add-test-source</goal>
      </goals>
      <configuration>
        <sources>
          <source>src/test/unit/java</source>
          <source>src/test/integration/java</source>
        </sources>
      </configuration>
    </execution>
  </executions>
</plugin>

Adding Test Resource Directories into Maven

Integration tests typically require additional resources, such as configuration files and SQL scripts, to be loaded during the tests. To include these resources in the test classpath, I added the following configuration:

<execution>
  <id>add-test-resource</id>
  <phase>generate-resources</phase>
  <goals>
    <goal>add-test-resource</goal>
  </goals>
  <configuration>
    <resources>
      <resource>
        <directory>src/test/integration/resources</directory>
      </resource>
    </resources>
  </configuration>
</execution>

Conclusion

By splitting the source directories for unit and integration tests and configuring Maven to recognize them, I’ve successfully separated the concerns of different types of tests in my application. This approach improves the organization and scalability of my codebase, making it easier to maintain as my project grows.