For some reason I cannot get Maven 2 Surefire plugin to execute JUnit 4 test class.
public class SimpleTest {
@org.junit.Test
public void simple() {
System.out.println("foo");
}
}
However if I change this class to be JUnit-3 like, such as
public class SimpleTest extends junit.framework.TestCase {
public void testBar() {
System.out.println("bar");
}
@org.junit.Test
public void simple() {
System.out.println("foo");
}
}
then it gets executed. Here's what I've done:
- verified Maven version: Apache Maven 2.2.1 (r801777; 2009-08-06 20:16:01+0100)
- verified Surefire version: followed this advice
- verified Surefire version: checked Surefire jars in my
~/.m2/repository/org/apache/maven/surefire
-- all of them are either version 2.4.2 or 2.4.3 - done a
mvn dependency:tree | grep junit
to ensure I only depend on junit version 4.7
The module I am having this problem at doesn't have JUnit 3 tests.
Is there anything else I am missing?
I don't know what you mean by "can't execute," but does it help to explicitly set the includes used by the
maven-surefire-plugin
?Also, does running maven with the
-X
flag provide any useful information?For the benefit of Googlers, when I had this issue it was because I'd included a PowerMock dependency that pulled in TestNG, causing no [TestNG] tests to be detected by SureFire.
I used the m2eclipse "Dependency Hierarchy" tab of the POM editor to find the dependency and right-clicked to generate an exclusion (see XML below).
For completeness (and for those not using m2eclipse) here's the XML that excludes the dependency - I only came across this feature of Maven by seeing these tags generated automatically:
(In my case, excluding "powermock-module-testng" was sufficient, but you could exclude TestNG directly if it's coming in from somewhere else.)
1.) Include following surefire plugin in pom.xml
2.)By default surefire plugin picks Test class from package :- src/test/java/....
So go to Build Path and include test folder in classpath like below :
3.) Go to --> Run As -->Maven Test
Have you configured your maven-compile-plugin for the correct compiler level, like:
Otherwise maven will have trouble with annotations
The Surefire plugin figures out which JUnit provider should be used based upon the classpath. If there are multiple JUnit versions on the classpath, you can either correct the classpath to have only one JUnit version on the classpath (as discussed above), or you can explicitly specify which provider you want to use. For example, specifying the following in your (parent) POM forces using the newest provider (e.g., "surefire-junit47"):
Note however that Surefire 2.7 changed the way it's determining which unit test classes are run. The new behavior when using Surefire 2.7 (or later) with JUnit 4 is that any test without a @Test annotation will be skipped automatically. This may be great if you just have JUnit 4 unit tests, but if you have a combination of JUnit 3 and 4 unit tests, using the "surefire-junit47" provider will not work correctly. In such cases, its best to explicitly choose the "surefire-junit4" provider:
One more possible cause can be this bug (closed with "Won't fix"): https://issues.apache.org/jira/browse/SUREFIRE-587
Short summary: Tests extending TestCase (but not using annotations) will not be picked up if their name does not end with "Test".