Unit Tests - Tests a small focused piece of code, if the method accesses an outside resource, database, or service, that interaction is mocked. Many different scenarios can be tested - macks can return a null, throw an exception, return the proper data... Please check out this mockito cheat sheet https://gist.github.com/kbrv37/b7b68432f32630a74ce9e936a9d49745
Integration Tests - Tests the same piece of code but with actual interactions. Usually limits your testing scenarios, but you can ensure it works with your particular service, database, api, or resource.
- Clarity for developer - when running test locally, the developer can see which tests require outside resources and avoid running them
- Automated Build Speed - you can make your automated builds dependent or independent of successful integration tests. You can keep the tests in the project, and only run when you'd like to verify they are successful.
- You can keep integration tests without running them, or adding @Disabled tags to them.
Separating tests is as simple as the following.
- Create a new folder next to
main
andtest
calledtestIntegration
- Create a subfolder in
testIntegration
calledjava
, right click java and clickMark Directory As -> Test Sources Root
- Add the following to your sections of the pom.xml
<skipTests>false</skipTests>
<skipUnitTests>${skipTests}</skipUnitTests>
<skipIntegrationTests>${skipTests}</skipIntegrationTests>
- Add the following plugins to your pom
<!-- plugin helps separate unit test from integration tests -->
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>3.0.0</version>
<executions>
<execution>
<id>add-test-source</id>
<phase>process-resources</phase>
<goals>
<goal>add-test-source</goal>
</goals>
<configuration>
<sources>
<source>src/testIntegration/java</source>
</sources>
</configuration>
</execution>
</executions>
</plugin>
<!-- surefire only runs unit tests (tests without resources) -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0-M5</version>
<configuration>
<excludes>
<exclude>**/*IT.java</exclude>
<exclude>**/IT*.java</exclude>
<exclude>**/*ITCase.java</exclude>
</excludes>
<skipTests>${skipUnitTests}</skipTests>
</configuration>
</plugin>
<!-- failsafe only runs integration tests (tests which interact with db, api, external service etc...) -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<version>3.0.0-M5</version>
<configuration>
<useFile>false</useFile>
<includes>
<include>**/*IT.java</include>
<include>**/IT*.java</include>
<include>**/*ITCase.java</include>
<include>**/*IntegrationTest.java</include>
<include>**/*TestIntegration.java</include>
</includes>
<skipITs>${skipIntegrationTests}</skipITs>
</configuration>
<executions>
<execution>
<id>failsafe-integration-tests</id>
<phase>integration-test</phase>
<goals>
<goal>integration-test</goal>
<goal>verify</goal>
</goals>
</execution>
</executions>
</plugin>
- maven-failsafe - runs integration tests
- maven-surfire - runs unit tests
- build-helper-maven-plugin - adds extra ability to add a separate test sources root to your project
- Add tests to your
src/testIntegration/java
folder with the following suffix*IT.java
The build now operates in the follwoing manner.
mvn tests
-> runs unit testsmvn verify
-> runs unit and integration testsmvn integration-test -DskipUnitTests
-> runs integration testsmvn verify -DskipTests
-> skips unit and integration testsmvn verify -DskipUnitTests
-> skips unit tests, but runs integration testsmvn verify -DskipIntegrationTests
-> skips integartion tests, but runs unit tests