Wednesday, March 2, 2016

How to separate integration test from unit test

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.

image

The project hierrarchy in Eclipse looks like below.

image

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)

image

The click button  "Brower" for Folder name.

 image

Select /src/integration/java, then OK, to add this directory to Eclipse build path.

image

Now you should see a new source folder added to you project in Eclipse.

image

Repeat to add another source folder /src/integration/resources. Now you project in Eclipse should looks like below.

image

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

image

Change Output folder for both /src/integration/java and /src/integration/resources to target/test-classes

image

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.

image

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.

image

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!

1 comment:

  1. 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

Powered by Blogger.

About The Author

My Photo
Has been a senior software developer, project manager for 10+ years. Dedicate himself to Alcatel-Lucent and China Telecom for delivering software solutions.

Pages

Unordered List