package de.superx; import java.io.File; import java.net.URL; import java.net.URLClassLoader; import org.junit.Ignore; import org.junit.runner.JUnitCore; import org.junit.runner.Result; import org.junit.runner.notification.Failure; /** * Class to run JUnit tests manually * @author hiber * */ @Ignore public class JUnitTestRunner { private final String pathToTestSources = String.join(File.separator, "test-src"); public static void main(String[] args) throws Exception { new JUnitTestRunner().execute("de.superx.stat.ExaminationStatistics2016Test", "de.superx.stat.GuestListenerStatisticsTest"); // new JUnitTestRunner().executePackage("de.superx.stat"); } private void executePackage(String packageName) throws Exception { String folderName = (this.pathToTestSources +"."+ packageName+".").replace(".", "/"); File[] files = new File("./"+folderName).listFiles(); for (File file : files) { if(file.getName().endsWith(".java")) { String binaryClassName = packageName + "."+file.getName().replace(".java", ""); this.execute(binaryClassName); } } } private void execute(String... binaryClassNames) throws Exception { for (String binaryClassName : binaryClassNames) { execute(binaryClassName); } } private void execute(String binaryClassName) throws Exception { String fileName = this.pathToTestSources + binaryClassName.replace(".", "/") + ".java"; URL[] urls = { new File(fileName).toURI().toURL() }; URLClassLoader urlClassLoader = new URLClassLoader(urls); Class testClasses = urlClassLoader.loadClass(binaryClassName); JUnitCore jUnitCore = new JUnitCore(); this.printResult(binaryClassName, jUnitCore.run(testClasses)); urlClassLoader.close(); } @SuppressWarnings("static-method") private void printResult(String binaryClassName, Result result) { System.out.println("Result of test "+binaryClassName+":"); System.out.println(">>> Tests:\t"+result.getRunCount()); System.out.println(">>> Skipped:\t"+result.getIgnoreCount()); System.err.println(">>> Failed:\t"+result.getFailureCount()); if (result.getFailureCount() > 0) { for (Failure failure : result.getFailures()) { System.err.println(">>> "+failure.toString()); } } } }