Index: surefire-junit/src/main/java/org/apache/maven/surefire/junit/JUnitDirectoryTestSuite.java =================================================================== --- surefire-junit/src/main/java/org/apache/maven/surefire/junit/JUnitDirectoryTestSuite.java (revision 452990) +++ surefire-junit/src/main/java/org/apache/maven/surefire/junit/JUnitDirectoryTestSuite.java (working copy) @@ -23,6 +23,7 @@ import org.apache.maven.surefire.testset.TestSetFailedException; import java.io.File; +import java.lang.reflect.Method; import java.util.ArrayList; /** @@ -50,9 +51,11 @@ { // ignore this } + + Method suiteMethod = JUnitTestSet.getSuiteMethod(testClass); SurefireTestSet testSet; - if ( junitClass != null && junitClass.isAssignableFrom( testClass ) ) + if ( junitClass != null && (junitClass.isAssignableFrom( testClass ) || suiteMethod != null) ) { testSet = new JUnitTestSet( testClass ); } Index: surefire-junit/src/main/java/org/apache/maven/surefire/junit/JUnitTestSet.java =================================================================== --- surefire-junit/src/main/java/org/apache/maven/surefire/junit/JUnitTestSet.java (revision 452990) +++ surefire-junit/src/main/java/org/apache/maven/surefire/junit/JUnitTestSet.java (working copy) @@ -26,6 +26,8 @@ import java.lang.reflect.Modifier; import java.lang.reflect.Proxy; +import junit.framework.Test; + public final class JUnitTestSet extends AbstractTestSet { @@ -76,7 +78,11 @@ { try { - Class testClass = getTestClass(); + Class testClass = null; + if(getSuiteMethod(getTestClass()) != null) + testClass = getSuiteMethod(getTestClass()).getReturnType(); + else + testClass = getTestClass(); ClassLoader loader = testClass.getClassLoader(); testResultClass = loader.loadClass( TEST_RESULT ); @@ -163,7 +169,32 @@ } return testObject; } + + /** + * Checks the specified testClass (which does not have to derive from {@link junit.framework.TestCase} to see if it contains a public, static suite() method that returns a value of type {@link Test} and returns that method if it is present. + * + * @param testClass the class to check + * @return a pointer to the suite() method if it is present or null if no such method can be found + */ + static Method getSuiteMethod(Class testClass) + { + try + { + Method suiteMethod = testClass.getMethod( "suite", EMPTY_CLASS_ARRAY ); + if ( Modifier.isPublic( suiteMethod.getModifiers() ) && Modifier.isStatic( suiteMethod.getModifiers() ) && Test.class.isAssignableFrom(suiteMethod.getReturnType())) + { + return suiteMethod; + } + } + catch ( NoSuchMethodException e ) + { + // No suite method found + } + + return null; + } + private static Object createInstanceFromSuiteMethod( Class testClass ) throws IllegalAccessException, InvocationTargetException { Index: surefire-junit/src/main/java/org/apache/maven/surefire/junit/TestListenerInvocationHandler.java =================================================================== --- surefire-junit/src/main/java/org/apache/maven/surefire/junit/TestListenerInvocationHandler.java (revision 452990) +++ surefire-junit/src/main/java/org/apache/maven/surefire/junit/TestListenerInvocationHandler.java (working copy) @@ -157,6 +157,13 @@ private void handleAddError( Object[] args ) throws IllegalAccessException, NoSuchMethodException, InvocationTargetException { + // Just a quick check to make sure that the "error" isn't really a JUnit4 AssertionError (which is actually just a failure) + if(args[1].getClass().getCanonicalName().equals("java.lang.AssertionError")) + { + handleAddFailure(args); + return; + } + ReportEntry report = new ReportEntry( args[0], args[0].toString(), args[1].toString(), getStackTraceWriter( args ) ); @@ -168,8 +175,17 @@ private JUnitStackTraceWriter getStackTraceWriter( Object[] args ) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException { - Method m = args[0].getClass().getMethod( "getName", EMPTY_CLASS_ARRAY ); - String testName = (String) m.invoke( args[0], EMPTY_STRING_ARRAY ); + String testName = null; + try + { + Method m = args[0].getClass().getMethod( "getName", EMPTY_CLASS_ARRAY ); + testName = (String) m.invoke( args[0], EMPTY_STRING_ARRAY ); + } + catch(NoSuchMethodException e) + { + // If the test case is really an instance of junit.framework.JUnit4TestCaseFacade it has no getName() method-- the test's name can be found by just calling toString() + testName = args[0].toString(); + } return new JUnitStackTraceWriter( args[0].getClass().getName(), testName, (Throwable) args[1] ); }