Enterprise Java

Integration tests with Maven

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 times they have pretty different requirements. For example it is ok to have your unit tests operate on a 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. Same structure with the test directory.

Then we should give instruction 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>

By using the build helper maven plugin we can instruct maven to add extra test source directories to our our build.

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.

mvn verify

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 on Java Code Geeks with permission by Emmanouil Gkatziouras, partner at our JCG program. See the original article here: Integration tests with maven

Opinions expressed by Java Code Geeks contributors are their own.

Emmanouil Gkatziouras

He is a versatile software engineer with experience in a wide variety of applications/services.He is enthusiastic about new projects, embracing new technologies, and getting to know people in the field of software.
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Inline Feedbacks
View all comments
Back to top button