0. Environment
- Maven 3.2 +
- Eclipse
- JUnit 4.+
This article supposes you already know how to use maven plugin failsafe for integration test.
1. Final goal
Let's clarify the word "separate" first. It means:
- Unit tests and integration tests are in different directories, but stay in the same maven project (not in another maven module project).
Also we want achieve these goals:
- In Eclipse, you can choose to only run unit tests alone, without integration tests
- In Eclipse, you can choose to only run integration tests alone, without unit tests
- In maven command line, you can choose to only run unit tests alone, without integration tests (mvn test)
- In maven command line, you can choose to only run integration tests alone, without unit tests (mvn clean test-compile failsafe:integration-test)
- In maven command line, you can also run them both with a single command. (mvn verify)
The final directory hierrarchy looks like below.
The project hierrarchy in Eclipse looks like below.
Just remember Eclipse and Maven have different ways to manage build process, so both Eclipse and Maven pom file need to be configured respectively.
2. Create directories for integration test
Create 2 directories in your project
- src/integration/java
- src/integration/resources
3. Configure in Eclipse
3.1 add source folder
In Eclipse, right click you project's name -> new -> Source Folder. ( Not folder, but Source Folder)
The click button "Brower" for Folder name.
Select /src/integration/java, then OK, to add this directory to Eclipse build path.
Now you should see a new source folder added to you project in Eclipse.
Repeat to add another source folder /src/integration/resources. Now you project in Eclipse should looks like below.
3.2 Config Eclipse build path
Since integration tests are also tests, you don't want them mixed with main code. So you need to change the "Build Path" in Eclipse. By default, newly added source folders are treated as main code/resources.
Right click project name -> Build Path -> Configure Build Path
Change Output folder for both /src/integration/java and /src/integration/resources to target/test-classes
Up to now, you should be about to run unit test and integration tests separately, by choose different dir to run.
4. Configure pom.xml
By default, maven can only have 1 test source directory, which point to ${project.basedir}/src/test/java. To add our newly created dir to maven as test source/resource, we need to utilize plugin 'build-helper'.
4.1 config failsafe plugin
Add failsafe plugin in <build>
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-failsafe-plugin</artifactId> <version>2.19.1</version> <configuration> <encoding>UTF-8</encoding> </configuration> <executions> <execution> <id>integration-test</id> <goals> <goal>integration-test</goal> <goal>verify</goal> </goals> </execution> </executions> </plugin>
4.2 config build-helper plugin
Add build-helper plugin to <build>.
<plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>build-helper-maven-plugin</artifactId> <version>1.9.1</version> <executions> <execution> <id>add-test-source</id> <goals> <goal>add-test-source</goal> </goals> <configuration> <sources> <source>${project.basedir}\src\integration\java</source> </sources> </configuration> </execution> <execution> <id>add-test-resource</id> <goals> <goal>add-test-resource</goal> </goals> <configuration> <resources> <resource> <!-- Don't forget <directory> label --> <directory>${project.basedir}\src\integration\resources</directory> </resource> </resources> </configuration> </execution> </executions> </plugin>
Now maven should also be happy with separated integration tests. Run command
mvn test
The unit test cases are invoked.
You can also just run integration test cases without packaging or running unit tests . (For example when you are writing integration test cases).
mvn clean test-compile failsafe:integration-test
You can also run both unit test cases and integration test cases with command.
mvn verify
4.3 Errors may occure in Eclipse
After adding plugin 'build-helper', Eclipse may report errors complain about "execution not covered", just following the autofix from Eclipse.
This will add Eclipse's maven lifecycle mapping configuration, which stored in file <Eclipse workspace>/.metadata/.plugins/org.eclipse.m2e.core/lifecycle-mapping-metadata.xml
You can empty this file to undo your change later.
Happy testing!
How about if you don't want to run the integration tests by default? Right now if you just do mvn package both sets of test run by default
ReplyDelete