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.
Join the DZone community and get the full member experience.Join For Free
Learn more about how CareerBuilder was able to resolve customer issues 5x faster by using Scalyr, the fastest log management tool on the market.
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:
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.
Published at DZone with permission of Emmanouil Gkatziouras , DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.