use of org.junit.internal.AssumptionViolatedException in project junit4 by junit-team.
the class ExpectExceptionTest method whenExpectingAssumptionViolatedExceptionStatementsThrowingItShouldPass.
@Test
public void whenExpectingAssumptionViolatedExceptionStatementsThrowingItShouldPass() {
Statement delegate = new Fail(new AssumptionViolatedException("expected"));
ExpectException expectException = new ExpectException(delegate, AssumptionViolatedException.class);
try {
expectException.evaluate();
// then AssumptionViolatedException should not be thrown
} catch (Throwable e) {
// need to explicitly catch and re-throw as an AssertionError or it might be skipped
fail("should not throw anything, but was thrown: " + e);
}
}
use of org.junit.internal.AssumptionViolatedException in project junit4 by junit-team.
the class MultipleFailureExceptionTest method assertEmptyWrapsAssumptionFailuresForManyThrowables.
@Test
public void assertEmptyWrapsAssumptionFailuresForManyThrowables() throws Exception {
List<Throwable> errors = new ArrayList<Throwable>();
AssumptionViolatedException assumptionViolatedException = new AssumptionViolatedException("skip it");
errors.add(assumptionViolatedException);
errors.add(new RuntimeException("garlic"));
try {
MultipleFailureException.assertEmpty(errors);
fail();
} catch (MultipleFailureException expected) {
assertThat(expected.getFailures().size(), equalTo(2));
assertTrue(expected.getMessage().startsWith("There were 2 errors:" + LINE_SEPARATOR));
assertTrue(expected.getMessage().contains("TestCouldNotBeSkippedException(Test could not be skipped"));
assertTrue(expected.getMessage().contains("RuntimeException(garlic)"));
Throwable first = expected.getFailures().get(0);
assertThat(first, instanceOf(TestCouldNotBeSkippedException.class));
Throwable cause = ((TestCouldNotBeSkippedException) first).getCause();
assertThat(cause, instanceOf(AssumptionViolatedException.class));
assertThat((AssumptionViolatedException) cause, CoreMatchers.sameInstance(assumptionViolatedException));
}
}
use of org.junit.internal.AssumptionViolatedException in project randomizedtesting by randomizedtesting.
the class RandomizedRunner method runSuite.
/**
* Test execution logic for the entire suite, executing under designated
* {@link RunnerThreadGroup}.
*/
private void runSuite(final RandomizedContext context, final RunNotifier notifier) {
final Result result = new Result();
final RunListener accounting = result.createListener();
notifier.addListener(accounting);
final Randomness classRandomness = runnerRandomness.clone(Thread.currentThread());
context.push(classRandomness);
try {
// Check for automatically hookable listeners.
subscribeListeners(notifier);
// Fire a synthetic "suite started" event.
for (RunListener r : autoListeners) {
try {
r.testRunStarted(suiteDescription);
} catch (Throwable e) {
logger.log(Level.SEVERE, "Panic: RunListener hook shouldn't throw exceptions.", e);
}
}
// Filter out test candidates to see if there's anything left.
// If not, don't bother running class hooks.
final List<TestCandidate> tests = getFilteredTestCandidates(notifier);
if (!tests.isEmpty()) {
Map<TestCandidate, Boolean> ignored = determineIgnoredTests(tests);
if (ignored.size() == tests.size()) {
// All tests ignored, ignore class hooks but report all the ignored tests.
for (TestCandidate c : tests) {
if (ignored.get(c)) {
reportAsIgnored(notifier, groupEvaluator, c);
}
}
} else {
ThreadLeakControl threadLeakControl = new ThreadLeakControl(notifier, this);
Statement s = runTestsStatement(threadLeakControl.notifier(), tests, ignored, threadLeakControl);
s = withClassBefores(s);
s = withClassAfters(s);
s = withClassRules(s);
s = withCloseContextResources(s, LifecycleScope.SUITE);
s = threadLeakControl.forSuite(s, suiteDescription);
try {
s.evaluate();
} catch (Throwable t) {
t = augmentStackTrace(t, runnerRandomness);
if (t instanceof AssumptionViolatedException) {
// Fire assumption failure before method ignores. (GH-103).
notifier.fireTestAssumptionFailed(new Failure(suiteDescription, t));
// see Rants#RANT_3
for (final TestCandidate c : tests) {
notifier.fireTestIgnored(c.description);
}
} else {
fireTestFailure(notifier, suiteDescription, t);
}
}
}
}
} catch (Throwable t) {
notifier.fireTestFailure(new Failure(suiteDescription, t));
}
// Fire a synthetic "suite ended" event and unsubscribe listeners.
for (RunListener r : autoListeners) {
try {
r.testRunFinished(result);
} catch (Throwable e) {
logger.log(Level.SEVERE, "Panic: RunListener hook shouldn't throw exceptions.", e);
}
}
// Final cleanup.
notifier.removeListener(accounting);
unsubscribeListeners(notifier);
context.popAndDestroy();
}
use of org.junit.internal.AssumptionViolatedException in project randomizedtesting by randomizedtesting.
the class RandomizedRunner method runSingleTest.
/**
* Runs a single test in the "master" test thread.
*/
void runSingleTest(final RunNotifier notifier, final TestCandidate c, final ThreadLeakControl threadLeakControl) {
notifier.fireTestStarted(c.description);
try {
// Get the test instance.
final Object instance = c.instanceProvider.newInstance();
// Collect rules and execute wrapped method.
Statement s = new Statement() {
public void evaluate() throws Throwable {
invoke(c.method, instance);
}
};
s = wrapExpectedExceptions(s, c);
s = wrapBeforeAndAfters(s, c, instance);
s = wrapMethodRules(s, c, instance);
s = withCloseContextResources(s, LifecycleScope.TEST);
s = threadLeakControl.forTest(s, c);
s.evaluate();
} catch (Throwable e) {
e = augmentStackTrace(e);
if (e instanceof AssumptionViolatedException) {
notifier.fireTestAssumptionFailed(new Failure(c.description, e));
} else {
fireTestFailure(notifier, c.description, e);
}
} finally {
notifier.fireTestFinished(c.description);
}
}
use of org.junit.internal.AssumptionViolatedException in project randomizedtesting by randomizedtesting.
the class ReproduceInfoPrinter method testFailure.
@Override
@SuppressForbidden("Legitimate use of syserr.")
public void testFailure(Failure failure) throws Exception {
// Ignore assumptions.
if (failure.getException() instanceof AssumptionViolatedException) {
return;
}
final Description d = failure.getDescription();
final StringBuilder b = new StringBuilder();
b.append("FAILURE : ").append(d.getDisplayName()).append("\n");
b.append("Message : " + failure.getMessage() + "\n");
b.append("Reproduce: ");
new ReproduceErrorMessageBuilder(b).appendAllOpts(failure.getDescription());
b.append("\n");
b.append("Throwable:\n");
if (failure.getException() != null) {
TraceFormatting traces = new TraceFormatting();
try {
traces = RandomizedContext.current().getRunner().getTraceFormatting();
} catch (IllegalStateException e) {
// Ignore if no context.
}
traces.formatThrowable(b, failure.getException());
}
System.err.println(b.toString());
}
Aggregations