use of org.junit.runner.notification.RunNotifier in project ceylon by eclipse.
the class CeylonModuleRunner method makeModuleRunnerInNewJvm.
private void makeModuleRunnerInNewJvm(final ModuleSpecifier module) {
final Description description = Description.createTestDescription(getClass(), "Run " + module.module() + " in new JVM");
Runner runner = new Runner() {
@Override
public Description getDescription() {
return description;
}
@Override
public void run(RunNotifier notifier) {
notifier.fireTestStarted(description);
try {
String moduleName = module.module();
String version = Module.DEFAULT_MODULE_NAME.equals(moduleName) ? null : module.version();
String runClass = module.runClass();
if (runClass.isEmpty())
runClass = moduleName + ".run_";
runModuleInNewJvm(moduleName, version, runClass);
} catch (Exception x) {
x.printStackTrace();
notifier.fireTestFailure(new Failure(description, x));
}
notifier.fireTestFinished(description);
}
};
children.put(runner, description);
}
use of org.junit.runner.notification.RunNotifier in project ceylon by eclipse.
the class CeylonModuleRunner method createFailingTest.
void createFailingTest(List<Runner> moduleRunners, final String testName, final Exception ex) {
final Description description = Description.createTestDescription(getClass(), testName);
Runner runner = new Runner() {
@Override
public Description getDescription() {
return description;
}
@Override
public void run(RunNotifier notifier) {
notifier.fireTestStarted(description);
notifier.fireTestFailure(new Failure(description, ex));
notifier.fireTestFinished(description);
}
};
moduleRunners.add(runner);
}
use of org.junit.runner.notification.RunNotifier in project scout.rt by eclipse.
the class InteractiveTestSuite method run.
@Override
@SuppressWarnings({ "squid:S1181", "squid:S1166" })
public void run(final RunNotifier notifier) {
System.out.println("Started interactive test console. (Auto-closing in 30 seconds when no input is entered, assuming it is a ci-test-run)");
String lastLine = "";
while (true) {
try {
System.out.println("********");
System.out.println("[Enter fully qualified test class name, enter to repeat last test, '.' to exit]");
if (!checkIfHumanInterface()) {
return;
}
String line = new BufferedReader(new InputStreamReader(System.in)).readLine();
if (StringUtility.isNullOrEmpty(line)) {
if (!lastLine.isEmpty()) {
line = lastLine;
} else {
continue;
}
}
if (".".equalsIgnoreCase(line)) {
return;
}
lastLine = line;
Method runMethod;
try {
// NOSONAR
runMethod = m_annotatedClass.getMethod("run", Runner.class, RunNotifier.class);
} catch (Throwable t) {
runMethod = InteractiveTestSuite.class.getMethod("run", Runner.class, RunNotifier.class);
}
Class<?> testClass = Class.forName(line, true, m_annotatedClass.getClassLoader());
Runner runner = m_builder.safeRunnerForClass(testClass);
if (Modifier.isStatic(runMethod.getModifiers())) {
runMethod.invoke(null, runner, notifier);
} else {
runMethod.invoke(m_annotatedClass.newInstance(), runner, notifier);
}
} catch (Throwable ex) {
System.out.println("Cannot load test " + lastLine);
ex.printStackTrace(System.out);
}
}
}
use of org.junit.runner.notification.RunNotifier in project randomizedtesting by randomizedtesting.
the class SlaveMain method execute.
/**
* Execute tests.
*/
private void execute(Iterator<String> classNames) throws Throwable {
final RunNotifier fNotifier = new OrderedRunNotifier();
final Result result = new Result();
final Writer debug = debugMessagesFile == null ? new NullWriter() : new OutputStreamWriter(new FileOutputStream(debugMessagesFile), "UTF-8");
fNotifier.addListener(result.createListener());
fNotifier.addListener(new StreamFlusherDecorator(new NoExceptionRunListenerDecorator(new RunListenerEmitter(serializer)) {
@Override
protected void exception(Throwable t) {
warn("Event serializer exception.", t);
}
}));
fNotifier.addListener(new RunListener() {
public void testRunFinished(Result result) throws Exception {
debug(debug, "testRunFinished(T:" + result.getRunCount() + ";F:" + result.getFailureCount() + ";I:" + result.getIgnoreCount() + ")");
serializer.flush();
}
@Override
public void testRunStarted(Description description) throws Exception {
debug(debug, "testRunStarted(" + description + ")");
serializer.flush();
}
@Override
public void testStarted(Description description) throws Exception {
debug(debug, "testStarted(" + description + ")");
serializer.flush();
}
public void testFinished(Description description) throws Exception {
debug(debug, "testFinished(" + description + ")");
serializer.flush();
}
@Override
public void testIgnored(Description description) throws Exception {
debug(debug, "testIgnored(T:" + description + ")");
}
@Override
public void testFailure(Failure failure) throws Exception {
debug(debug, "testFailure(T:" + failure + ")");
}
@Override
public void testAssumptionFailure(Failure failure) {
try {
debug(debug, "testAssumptionFailure(T:" + failure + ")");
} catch (IOException e) {
throw new RuntimeException(e);
}
}
});
/*
* Instantiate method filter if any.
*/
String methodFilterGlob = Strings.emptyToNull(System.getProperty(SysGlobals.SYSPROP_TESTMETHOD()));
Filter methodFilter = Filter.ALL;
if (methodFilterGlob != null) {
methodFilter = new MethodGlobFilter(methodFilterGlob);
}
/*
* Important. Run each class separately so that we get separate
* {@link RunListener} callbacks for the top extracted description.
*/
debug(debug, "Entering main suite loop.");
try {
while (classNames.hasNext()) {
final String clName = classNames.next();
debug(debug, "Instantiating: " + clName);
Class<?> clazz = instantiate(clName);
if (clazz == null)
continue;
Request request = Request.aClass(clazz);
try {
Runner runner = request.getRunner();
methodFilter.apply(runner);
// New RunListener instances should be added per class and then removed from the RunNotifier
ArrayList<RunListener> runListenerInstances = instantiateRunListeners();
for (RunListener runListener : runListenerInstances) {
fNotifier.addListener(runListener);
}
fNotifier.fireTestRunStarted(runner.getDescription());
debug(debug, "Runner.run(" + clName + ")");
runner.run(fNotifier);
debug(debug, "Runner.done(" + clName + ")");
fNotifier.fireTestRunFinished(result);
for (RunListener runListener : runListenerInstances) {
fNotifier.removeListener(runListener);
}
} catch (NoTestsRemainException e) {
// Don't complain if all methods have been filtered out.
// I don't understand the reason why this exception has been
// built in to filters at all.
}
}
} catch (Throwable t) {
debug(debug, "Main suite loop error: " + t);
throw t;
} finally {
debug(debug, "Leaving main suite loop.");
debug.close();
}
}
use of org.junit.runner.notification.RunNotifier in project spring-framework by spring-projects.
the class JUnitTestingUtils method runTestsAndAssertCounters.
/**
* Run the tests in the supplied {@code testClass}, using the specified
* {@link Runner}, and assert the expectations of the test execution.
*
* <p>If the specified {@code runnerClass} is {@code null}, the tests
* will be run with the runner that the test class is configured with
* (i.e., via {@link RunWith @RunWith}) or the default JUnit runner.
*
* @param runnerClass the explicit runner class to use or {@code null}
* if the default JUnit runner should be used
* @param testClass the test class to run with JUnit
* @param expectedStartedCount the expected number of tests that started
* @param expectedFailedCount the expected number of tests that failed
* @param expectedFinishedCount the expected number of tests that finished
* @param expectedIgnoredCount the expected number of tests that were ignored
* @param expectedAssumptionFailedCount the expected number of tests that
* resulted in a failed assumption
*/
public static void runTestsAndAssertCounters(Class<? extends Runner> runnerClass, Class<?> testClass, int expectedStartedCount, int expectedFailedCount, int expectedFinishedCount, int expectedIgnoredCount, int expectedAssumptionFailedCount) throws Exception {
TrackingRunListener listener = new TrackingRunListener();
if (runnerClass != null) {
Constructor<?> constructor = runnerClass.getConstructor(Class.class);
Runner runner = (Runner) BeanUtils.instantiateClass(constructor, testClass);
RunNotifier notifier = new RunNotifier();
notifier.addListener(listener);
runner.run(notifier);
} else {
JUnitCore junit = new JUnitCore();
junit.addListener(listener);
junit.run(testClass);
}
assertSoftly(softly -> {
softly.assertThat(listener.getTestStartedCount()).as("tests started for [%s]", testClass).isEqualTo(expectedStartedCount);
softly.assertThat(listener.getTestFailureCount()).as("tests failed for [%s]", testClass).isEqualTo(expectedFailedCount);
softly.assertThat(listener.getTestFinishedCount()).as("tests finished for [%s]", testClass).isEqualTo(expectedFinishedCount);
softly.assertThat(listener.getTestIgnoredCount()).as("tests ignored for [%s]", testClass).isEqualTo(expectedIgnoredCount);
softly.assertThat(listener.getTestAssumptionFailureCount()).as("failed assumptions for [%s]", testClass).isEqualTo(expectedAssumptionFailedCount);
});
}
Aggregations