In any growing software application, the number of tests also grows—unit tests, integration tests, smoke tests, performance tests, security tests, and the list goes on. Running all these tests every time you make a small change can slow down the development process significantly. Imagine running integration and smoke tests along with unit tests every time; it would be a nightmare!
To speed up my development process for my blog application, I decided to split the tests into separate unit and integration profiles.
1. Split Test Classes Using Naming Conventions
I used naming conventions to distinguish between unit and integration tests. This way, I could configure Maven to run the appropriate tests based on their file names.
Integration Test Classes:
ApplicationIT
ModularityIT
Unit Test Classes:
PostServiceTest
UserServiceTest
By using the *IT
suffix for integration tests and *Test
for unit tests, I could easily identify and separate them later when configuring Maven.
2. Configuring Maven Profiles
By default, Maven uses the Surefire plugin to run tests, and it can be configured to include test files based on their postfixed names.
<profiles>
<!-- Unit Test Profile -->
<profile>
<id>unit</id> <!-- The profile ID "unit" is used to trigger unit tests -->
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<!-- Include classes ending with "Test" -->
<includes>**/*Test.java</includes>
</configuration>
</plugin>
</plugins>
</build>
</profile>
<!-- Integration Test Profile -->
<profile>
<id>integration</id> <!-- The profile ID "integration" is used to trigger integration tests -->
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<!-- Include classes ending with "IT" for Integration Tests -->
<includes>**/*IT.java</includes>
</configuration>
</plugin>
</plugins>
</build>
</profile>
</profiles>
In this setup:
- Unit Profile: This profile runs all classes that end with
*Test.java
. These are typically your unit test classes. - Integration Profile: This profile runs all classes that end with
*IT.java
. These are your integration test classes.
3. Setting the Default Profile
By default, Maven will use the first profile in the configuration but it is also possible to configure it explicitly.
<activation>
<activeByDefault>true</activeByDefault>
</activation>
4. Running Tests with Different Profiles
# Run tests without specifying a profile
mvn clean test
If you don’t specify a profile, the unit tests will run by default. To run the integration tests (or any other profile), simply include the -P
flag in the Maven command:
# Run integration tests using the integration profile
mvn clean test -P integration
This command runs only the integration tests (*IT.java
), allowing you to run integration tests when you need.
Conclusion
By splitting tests into different profiles using Maven, I significantly reduced the time it takes to run tests during development. The separation also makes it easier to manage different types of tests without the need to constantly run everything. Whether you're running fast unit tests or slower integration tests, this setup provides flexibility and control over the testing process.