Skip to content

Instantly share code, notes, and snippets.

@d-j-kendall
Last active June 20, 2021 23:21
Show Gist options
  • Save d-j-kendall/91ed1b10772fa38a427335dbe2056c62 to your computer and use it in GitHub Desktop.
Save d-j-kendall/91ed1b10772fa38a427335dbe2056c62 to your computer and use it in GitHub Desktop.
Separating Unit Tests From Integration Tests (Tested with Junit5)

Definition

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.

Benefits of separating unit tests and integration tests.

  1. Clarity for developer - when running test locally, the developer can see which tests require outside resources and avoid running them
  2. 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.
  3. You can keep integration tests without running them, or adding @Disabled tags to them.

How To Separate Tests in Maven Java project

Separating tests is as simple as the following.

  1. Create a new folder next to main and test called testIntegration
  2. Create a subfolder in testIntegration called java, right click java and click Mark Directory As -> Test Sources Root
  3. Add the following to your sections of the pom.xml
      <skipTests>false</skipTests>
      <skipUnitTests>${skipTests}</skipUnitTests>
      <skipIntegrationTests>${skipTests}</skipIntegrationTests>
  1. 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>
            

Plugins

  • 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
  1. Add tests to your src/testIntegration/java folder with the following suffix *IT.java

The build now operates in the follwoing manner.

  1. mvn tests -> runs unit tests
  2. mvn verify -> runs unit and integration tests
  3. mvn integration-test -DskipUnitTests -> runs integration tests
  4. mvn verify -DskipTests -> skips unit and integration tests
  5. mvn verify -DskipUnitTests -> skips unit tests, but runs integration tests
  6. mvn verify -DskipIntegrationTests -> skips integartion tests, but runs unit tests
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment