Skip to content

Commit

Permalink
test: Do not use JUnitCore.main()
Browse files Browse the repository at this point in the history
Use JUnitCore.runClasses() instead, per JUnit docs.
  • Loading branch information
rhwood committed Mar 4, 2019
1 parent 96ce34f commit e7d516d
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 12 deletions.
2 changes: 1 addition & 1 deletion java/test/apps/tests/AllTest.java
Expand Up @@ -36,7 +36,7 @@ public static void initLogging() {

static public void main(String[] args) {
// launch this class via JUnit4
org.junit.runner.JUnitCore.main("apps.tests.AllTest");
org.junit.runner.JUnitCore.runClasses(AllTest.class);
}

}
3 changes: 1 addition & 2 deletions java/test/jmri/HeadLessTest.java
Expand Up @@ -34,7 +34,6 @@ static public void main(String[] args) {
System.setProperty("java.awt.headless", "true");

// start tests
String[] testCaseName = {"-noloading", HeadLessTest.class.getName()};
junit.textui.TestRunner.main(testCaseName);
org.junit.runner.JUnitCore.runClasses(HeadLessTest.class);
}
}
25 changes: 16 additions & 9 deletions java/test/jmri/util/junit/TestClassMainMethod.java
Expand Up @@ -2,6 +2,8 @@

import java.lang.reflect.*;

import org.junit.runner.JUnitCore;

/**
* Main method to launch a JUnit test class
*
Expand All @@ -25,16 +27,21 @@ static public void main(String[] args) {
className = className.replace("..",".");

try {
// first try to find a main in the class
Class<?> cl = Class.forName(className);
Method method = cl.getMethod("main", String[].class);
method.invoke(null, new Object[] {new String[] { /* put args here */ }});
} catch (InvocationTargetException e) {
// main threw an exception, report
System.err.println(e);
} catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException e) {
// failed, now invoke manually
org.junit.runner.JUnitCore.main(className);
// first try to find a main in the class
try {
Method method = cl.getMethod("main", String[].class);
method.invoke(null, new Object[] {new String[] { /* put args here */ }});
} catch (InvocationTargetException e) {
// main threw an exception, report
System.err.println(e);
} catch (NoSuchMethodException | IllegalAccessException e) {
// failed, now invoke manually
JUnitCore.runClasses(cl);
}
} catch (ClassNotFoundException e) {
// log error
System.err.println("Unable to locate class " + className);
}
}
}

0 comments on commit e7d516d

Please sign in to comment.