Search in sources :

Example 6 with AssumptionViolatedException

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);
    }
}
Also used : AssumptionViolatedException(org.junit.internal.AssumptionViolatedException) Statement(org.junit.runners.model.Statement) Test(org.junit.Test)

Example 7 with AssumptionViolatedException

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));
    }
}
Also used : AssumptionViolatedException(org.junit.internal.AssumptionViolatedException) MultipleFailureException(org.junit.runners.model.MultipleFailureException) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Example 8 with 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();
}
Also used : AssumptionViolatedException(org.junit.internal.AssumptionViolatedException) Statement(org.junit.runners.model.Statement) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Failure(org.junit.runner.notification.Failure) Result(org.junit.runner.Result) RunListener(org.junit.runner.notification.RunListener)

Example 9 with AssumptionViolatedException

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);
    }
}
Also used : AssumptionViolatedException(org.junit.internal.AssumptionViolatedException) Statement(org.junit.runners.model.Statement) Failure(org.junit.runner.notification.Failure)

Example 10 with AssumptionViolatedException

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());
}
Also used : Description(org.junit.runner.Description) AssumptionViolatedException(org.junit.internal.AssumptionViolatedException) SuppressForbidden(com.carrotsearch.randomizedtesting.annotations.SuppressForbidden)

Aggregations

AssumptionViolatedException (org.junit.internal.AssumptionViolatedException)26 Test (org.junit.Test)7 Statement (org.junit.runners.model.Statement)6 EachTestNotifier (org.junit.internal.runners.model.EachTestNotifier)4 Failure (org.junit.runner.notification.Failure)4 URI (java.net.URI)3 Description (org.junit.runner.Description)3 MultipleFailureException (org.junit.runners.model.MultipleFailureException)3 FileStatus (org.apache.hadoop.fs.FileStatus)2 Path (org.apache.hadoop.fs.Path)2 SuppressForbidden (com.carrotsearch.randomizedtesting.annotations.SuppressForbidden)1 BufferedReader (java.io.BufferedReader)1 FileInputStream (java.io.FileInputStream)1 FileNotFoundException (java.io.FileNotFoundException)1 IOException (java.io.IOException)1 InputStreamReader (java.io.InputStreamReader)1 PrintWriter (java.io.PrintWriter)1 Reader (java.io.Reader)1 ArrayList (java.util.ArrayList)1 Locale (java.util.Locale)1