public static void main(String[] args) throws Exception { ClassLoader.getSystemClassLoader().setDefaultAssertionStatus(true); // Enable asserts new ProcessFiles(new AtUnit(), "class").start(args); if (failures == 0) Print.print("OK (" + testsRun + " tests)"); else { Print.print("(" + testsRun + " tests)"); Print.print("\n>>> " + failures + " FAILURE" + (failures > 1 ? "S" : "") + " <<<"); for (String failed : failedTests) Print.print(" " + failed); } }
public void process(File cFile) { try { String cName = ClassNameFinder.thisClass(BinaryFile.read(cFile)); if (!cName.contains("")) return; // Ignore unpackaged classes testClass = Class.forName(cName); } catch (Exception e) { throw new RuntimeException(e); } TestMethods testMethods = new TestMethods(); Method creator = null; Method cleanup = null; for (Method m : testClass.getDeclaredMethods()) { testMethods.addIfTestMethod(m); if (creator == null) creator = checkForCreatorMethod(m); if (cleanup == null) cleanup = checkForCleanupMethod(m); } if (testMethods.size() > 0) { if (creator == null) try { if (!Modifier.isPublic(testClass.getDeclaredConstructor().getModifiers())) { Print.print("Error: " + testClass + " default constructor must be public"); System.exit(1); } } catch (NoSuchMethodException e) { // Synthesized default constructor; OK } Print.print(testClass.getName()); } for (Method m : testMethods) { Print.printnb(" . " + m.getName() + " "); try { Object testObject = createTestObject(creator); boolean success = false; try { if (m.getReturnType().equals(boolean.class)) success = (Boolean) m.invoke(testObject); else { m.invoke(testObject); success = true; // If no assert fails } } catch (InvocationTargetException e) { // Actual exception is inside e: Print.print(e.getCause()); } Print.print(success ? "" : "(failed)"); testsRun++; if (!success) { failures++; failedTests.add(testClass.getName() + ": " + m.getName()); } if (cleanup != null) cleanup.invoke(testObject, testObject); } catch (Exception e) { throw new RuntimeException(e); } } }