CVS: junit/src/org/junit/internal/requests ClassRequest.java, 1.3, 1.4

David Saff <[email protected]>
Newsgroups gmane.comp.java.junit.devel
Message-ID <[email protected]>
Update of /cvsroot/junit/junit/src/org/junit/internal/requests
In directory sc8-pr-cvs6.sourceforge.net:/tmp/cvs-serv11377/src/org/junit/internal/requests

Modified Files:
	ClassRequest.java 
Log Message:
Better error message when a custom runner has a wrongly-signatured constructor

Index: ClassRequest.java
===================================================================
RCS file: /cvsroot/junit/junit/src/org/junit/internal/requests/ClassRequest.java,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -d -r1.3 -r1.4
--- ClassRequest.java	24 Jan 2007 16:32:59 -0000	1.3
+++ ClassRequest.java	21 Feb 2007 17:12:37 -0000	1.4
@@ -1,8 +1,7 @@
 package org.junit.internal.requests;
 
-import java.lang.reflect.Constructor;
-
 import org.junit.Ignore;
+import org.junit.internal.runners.InitializationError;
 import org.junit.internal.runners.OldTestClassRunner;
 import org.junit.internal.runners.TestClassRunner;
 import org.junit.runner.Request;
@@ -11,6 +10,7 @@
 import org.junit.runners.AllTests;
 
 public class ClassRequest extends Request {
+	private static final String CONSTRUCTOR_ERROR_FORMAT= "Custom runner class %s should have a public constructor with signature %s(Class testClass)";
 	private final Class<?> fTestClass;
 	private boolean fCanUseSuiteMethod;
 
@@ -25,15 +25,20 @@
 	
 	@Override
 	public Runner getRunner() {
-		Class<? extends Runner> runnerClass= getRunnerClass(fTestClass);
+		return buildRunner(getRunnerClass(fTestClass)); 
+	}
+
+	public Runner buildRunner(Class<? extends Runner> runnerClass) {
 		try {
-			Constructor<? extends Runner> constructor= runnerClass.getConstructor(Class.class); // TODO good error message if no such constructor
-			return constructor.newInstance(new Object[] { fTestClass });
-		} catch (StackOverflowError e) {
-			throw new RuntimeException();
+			return runnerClass.getConstructor(Class.class).newInstance(new Object[] { fTestClass });
+		} catch (NoSuchMethodException e) {
+			String simpleName= runnerClass.getSimpleName();
+			InitializationError error= new InitializationError(String.format(
+					CONSTRUCTOR_ERROR_FORMAT, simpleName, simpleName));
+			return Request.errorReport(fTestClass, error).getRunner();
 		} catch (Exception e) {
 			return Request.errorReport(fTestClass, e).getRunner();
-		} 
+		}
 	}
 
 	Class<? extends Runner> getRunnerClass(final Class<?> testClass) {


-------------------------------------------------------------------------
Take Surveys. Earn Cash. Influence the Future of IT
Join SourceForge.net's Techsay panel and you'll get the chance to share your
opinions on IT & business topics through brief surveys-and earn cash
http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.