Using Junit

Configuring Junit

To get started with junit, you need to add the required version of junit to your project:

  [...]
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.8.1</version>
      <scope>test</scope>
    </dependency>
  [...]

This is the only step that is required to get started - you can now create tests in your test source directory (eg, src/test/java).

Generations of junit support

Surefire supports three different generations of junit: Junit3.8.x, Junit 4.x (serial provider) and Junit 4.7 (junit-core provider with parallel support). The provider is selected based on the junit version in your project and the configuration parameters (for parallel).

How is the provider chosen ?

if the junit version in the project >= 4.7 and the parallel attribute has ANY value
    use junit47 provider
if junit >= 4.0 is present
    use junit4 provider
else
    use junit3.8.1

Please note that the "else" part of this algorithm is also a FAQ response:

You depend on the appropriate version of junit being present in the project dependencies, or surefire may choose the wrong provider. If, for instance, one of your dependencies pulls in junit 3.8.1 you risk that surefire chooses the 3.8.1 provider, which will not support annotations or any of the 4.x features.

Use mvn dependency:tree, pom dependency ordering and/or and exclusion of transitive dependencies to fix this problem.

Running tests in parallel

From junit 4.7 and onwards you can run your tests in parallel. To do this, you must set the parallel parameter, and may change the threadCount or useUnlimitedThreads attribute. For example:

    [...]
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-surefire-plugin</artifactId>
        <version>2.6</version>
        <configuration>
          <parallel>methods</parallel>
          <threadCount>10</threadCount>
        </configuration>
      </plugin>
    [...]

If your tests specify any value for the "parallel" attribute and your project uses junit4.7+, your request will be routed to the concurrent junit provider, which uses the JUnit JUnitCore testrunner.

This is particularly useful for slow tests that can have high concurrency.

The JUnitCore test-runner has stricter test-compliance checking than earlier versions of junit; with this provider your test must be legitimate junit tests to be run, whereas the old 4.x provider would also run tests that were not according to junit spec.

For more information on JUnit, see the Junit web site.