Maven JUnit4 plugin

Update

Update: The Surefire plugin version 2.3 has reportedly been upgraded to support JUnit4. Please try adding the following to your pom.xml before continuing on with the information found on this page.

  <plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.3</version>
</plugin>

You can also read the full announcement maven-users mailing list.

Thanks to Chris Moesel for passing along the good news!

Introduction

This Maven 2 plugin was developed to temporarily address a shortcoming in the standard Surefire plugin that prevents it from executing JUnit 4 test classes. This plugin is in no way intended to replace Surefire or even remain available after Surefire is upgraded. As such, no further work is expected to be done on this plugin.

Overview

The maven-junit4-plugin is executed during the test phase of the mvn lifecycle.

All JUnit 4 test classes that were built into the target/test-classes directory (the default test-compile output directory) will automatically be executed when the mvn test phase is run.

Limitations

The paths for the project's classes and the test classes are hard-coded to the maven default values of target/classes and target/test-classes.

The surefire plugin's test phase is not disabled. There is therefore spurious Surefire output during the test phase.

No attempt is made to run JUnit 3 tests.

The unit tests run in the same JVM as maven itself. There is no forking model.

The unit tests for the plugin itself (i.e., if you were modifying the source code for maven-junit4-plugin) can not be run natively inside Eclipse itself. You either need to test the plugin via maven or set up Eclipse to execute mvn directly.

Usage

The maven-junit4-plugin should require no modifications to a project other than to the pom.xml file itself.

Add the following to the project's pom.xml:

 <dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.0</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.5</source>
<target>1.5</target>
</configuration>
</plugin>
<plugin>
<groupId>net.unto.maven.plugins</groupId>
<artifactId>maven-junit4-plugin</artifactId>
<version>1.0-SNAPSHOT</version>
<executions>
<execution>
<phase>test</phase>
<goals>
<goal>test</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>

<pluginRepositories>
<pluginRepository>
<id>unto.net</id>
<url>http://repository.unto.net/maven/</url>
<releases>
<updatePolicy>daily</updatePolicy>
<checksumPolicy>warn</checksumPolicy>
</releases>
<snapshots>
<updatePolicy>daily</updatePolicy>
<checksumPolicy>warn</checksumPolicy>
</snapshots>
</pluginRepository>
</pluginRepositories>


The maven-junit4-plugin should download and install automatically when mvn test is run.

To run the plugin, execute:

 mvn test


Source Code

The pathological and curious alike can access the source code for the maven-junit4-plugin via the Unto.net svn repository.

To check out a current copy of the maven-junit4-plugin source code, please run:

 svn co http://svn.unto.net/svn/public/maven-junit4-plugin/trunk/ \
maven-junit4-plugin

Further Reading

Comments