Search in sources :

Example 1 with MultipleFailureException

use of org.junit.runners.model.MultipleFailureException in project junit4 by junit-team.

the class ExternalResourceRuleTest method shouldThrowMultipleFailureExceptionWhenTestFailsAndClosingResourceFails.

@Test
public void shouldThrowMultipleFailureExceptionWhenTestFailsAndClosingResourceFails() throws Throwable {
    // given
    ExternalResource resourceRule = new ExternalResource() {

        @Override
        protected void after() {
            throw new RuntimeException("simulating resource tear down failure");
        }
    };
    Statement failingTest = new Fail(new RuntimeException("simulated test failure"));
    Description dummyDescription = Description.createTestDescription("dummy test class name", "dummy test name");
    try {
        resourceRule.apply(failingTest, dummyDescription).evaluate();
        fail("ExternalResource should throw");
    } catch (MultipleFailureException e) {
        assertThat(e.getMessage(), allOf(containsString("simulated test failure"), containsString("simulating resource tear down failure")));
    }
}
Also used : Description(org.junit.runner.Description) MultipleFailureException(org.junit.runners.model.MultipleFailureException) Statement(org.junit.runners.model.Statement) Fail(org.junit.internal.runners.statements.Fail) Test(org.junit.Test)

Example 2 with MultipleFailureException

use of org.junit.runners.model.MultipleFailureException 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 3 with MultipleFailureException

use of org.junit.runners.model.MultipleFailureException in project junit4 by junit-team.

the class FailOnTimeout method createTimeoutException.

private Exception createTimeoutException(Thread thread) {
    StackTraceElement[] stackTrace = thread.getStackTrace();
    final Thread stuckThread = lookForStuckThread ? getStuckThread(thread) : null;
    Exception currThreadException = new TestTimedOutException(timeout, timeUnit);
    if (stackTrace != null) {
        currThreadException.setStackTrace(stackTrace);
        thread.interrupt();
    }
    if (stuckThread != null) {
        Exception stuckThreadException = new Exception("Appears to be stuck in thread " + stuckThread.getName());
        stuckThreadException.setStackTrace(getStackTrace(stuckThread));
        return new MultipleFailureException(Arrays.<Throwable>asList(currThreadException, stuckThreadException));
    } else {
        return currThreadException;
    }
}
Also used : MultipleFailureException(org.junit.runners.model.MultipleFailureException) TestTimedOutException(org.junit.runners.model.TestTimedOutException) TestTimedOutException(org.junit.runners.model.TestTimedOutException) TimeoutException(java.util.concurrent.TimeoutException) MultipleFailureException(org.junit.runners.model.MultipleFailureException) ExecutionException(java.util.concurrent.ExecutionException)

Example 4 with MultipleFailureException

use of org.junit.runners.model.MultipleFailureException in project randomizedtesting by randomizedtesting.

the class TestFailurePropagationCompatibility method maybeFail.

static void maybeFail() {
    if (random != null) {
        if (random.nextInt(frequency) == 0) {
            if (random.nextBoolean()) {
                throw new RuntimeException("State: " + random.nextLong());
            } else {
                ArrayList<Throwable> errors = new ArrayList<Throwable>();
                errors.add(new RuntimeException("State: " + random.nextLong()));
                errors.add(new RuntimeException("State: " + random.nextLong()));
                // Throw MultipleFailureException as if unchecked.
                Rethrow.rethrow(new MultipleFailureException(errors));
            }
        }
    }
}
Also used : MultipleFailureException(org.junit.runners.model.MultipleFailureException) ArrayList(java.util.ArrayList)

Example 5 with MultipleFailureException

use of org.junit.runners.model.MultipleFailureException in project spock by spockframework.

the class JUnitSupervisor method error.

public int error(ErrorInfo error) {
    Throwable exception = error.getException();
    if (exception instanceof MultipleFailureException)
        return handleMultipleFailures(error);
    if (isFailedEqualityComparison(exception))
        exception = convertToComparisonFailure(exception);
    filter.filter(exception);
    Failure failure = new Failure(getCurrentDescription(), exception);
    if (exception instanceof AssumptionViolatedException) {
        // do notify JUnit listeners unless it's a data-driven iteration that's reported as one feature
        if (currentIteration == null || !currentFeature.isParameterized() || currentFeature.isReportIterations()) {
            notifier.fireTestAssumptionFailed(failure);
        }
    } else {
        masterListener.error(error);
        notifier.fireTestFailure(failure);
    }
    errorSinceLastReset = true;
    return statusFor(error);
}
Also used : MultipleFailureException(org.junit.runners.model.MultipleFailureException) AssumptionViolatedException(org.junit.internal.AssumptionViolatedException) Failure(org.junit.runner.notification.Failure) ComparisonFailure(org.junit.ComparisonFailure)

Aggregations

MultipleFailureException (org.junit.runners.model.MultipleFailureException)11 ArrayList (java.util.ArrayList)5 Statement (org.junit.runners.model.Statement)4 Test (org.junit.Test)3 AssumptionViolatedException (org.junit.internal.AssumptionViolatedException)3 Description (org.junit.runner.Description)2 Failure (org.junit.runner.notification.Failure)2 Job (com.birbit.android.jobqueue.Job)1 JobManager (com.birbit.android.jobqueue.JobManager)1 Params (com.birbit.android.jobqueue.Params)1 IOException (java.io.IOException)1 ObjectInputStream (java.io.ObjectInputStream)1 Method (java.lang.reflect.Method)1 HttpURLConnection (java.net.HttpURLConnection)1 URL (java.net.URL)1 List (java.util.List)1 ExecutionException (java.util.concurrent.ExecutionException)1 TimeoutException (java.util.concurrent.TimeoutException)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1 JsonString (javax.json.JsonString)1