/** * Adds {@code matcher} to the list of requirements for the cause of * any thrown exception. */ public void expectCause(Matcher<? extends Throwable> expectedCause) { expect(hasCause(expectedCause)); }
/** * Returns a matcher that verifies that the outer exception has a cause for which the supplied matcher * evaluates to true. * * @param matcher to apply to the cause of the outer exception * @param <T> type of the outer exception */ @Factory public static <T extends Throwable> Matcher<T> hasCause(final Matcher<?> matcher) { return new ThrowableCauseMatcher<T>(matcher); } }
/** * Verify that your code throws an exception whose cause is matched by * a Hamcrest matcher. * <pre> @Test * public void throwsExceptionWhoseCauseCompliesWithMatcher() { * NullPointerException expectedCause = new NullPointerException(); * thrown.expectCause(is(expectedCause)); * throw new IllegalArgumentException("What happened?", cause); * }</pre> * * @deprecated use {@code org.hamcrest.junit.ExpectedException.expectCause()} */ @Deprecated public void expectCause(Matcher<?> expectedCause) { expect(hasCause(expectedCause)); }
@Factory public static <T extends Throwable> Matcher<T> hasCause(final Matcher<T> matcher) { return new ThrowableCauseMatcher<T>(matcher); } }
@Test(timeout = 10000) public void openStream_network_errors() throws IOException, URISyntaxException { // non routable address String url = "http://10.255.255.1"; thrown.expect(SonarException.class); thrown.expect(hasCause(new BaseMatcher<Exception>() { @Override public boolean matches(Object ex) { return // Java 8 ex instanceof NoRouteToHostException || ex instanceof SocketException // Java 7 or before || ex instanceof SocketTimeoutException; } @Override public void describeTo(Description arg0) { } })); DefaultHttpDownloader downloader = new DefaultHttpDownloader(new MapSettings().asConfig(), 10, 50000); downloader.openStream(new URI(url)); }
@Factory public static <T extends Throwable> Matcher<T> hasCause(final Matcher<T> matcher) { return new ThrowableCauseMatcher<T>(matcher); } }
/** * Adds {@code matcher} to the list of requirements for the cause of * any thrown exception. */ public void expectCause(Matcher<? extends Throwable> expectedCause) { expect(hasCause(expectedCause)); }
@Factory public static <T extends Throwable> Matcher<T> hasCause(final Matcher<T> matcher) { return new ThrowableCauseMatcher<T>(matcher); } }
@Test public void processError() throws Exception { thrown.expectCause((is(instanceOf(MessagingException.class)))); thrown.expectCause(hasCause(is(exception))); try { from(MessageProcessors.process(input, error)).block(); } finally { assertThat(from(responsePublisher).toFuture().isDone(), is(true)); from(responsePublisher).toFuture() .whenComplete((event, throwable) -> assertThat(throwable.getCause(), equalTo(exception))); } }
/** * Returns a matcher that verifies that the outer exception has a cause for which the supplied matcher * evaluates to true. * * @param matcher to apply to the cause of the outer exception * @param <T> type of the outer exception */ @Factory public static <T extends Throwable> Matcher<T> hasCause(final Matcher<? extends Throwable> matcher) { return new ThrowableCauseMatcher<T>(matcher); } }
@Test public void processErrorInFlow() throws Exception { try { from(MessageProcessors.process(input, createFlow(error))).block(); fail("Exception expected"); } catch (Throwable t) { assertThat(t.getCause(), is(instanceOf(MessagingException.class))); assertThat(t.getCause().getCause(), is(exception)); } assertThat(from(responsePublisher).toFuture().isDone(), is(true)); thrown.expectCause((is(instanceOf(MessagingException.class)))); thrown.expectCause(hasCause(is(exception))); from(responsePublisher).block(); }
@Factory public static <T extends Throwable> Matcher<T> hasCause(final Matcher<T> matcher) { return new ThrowableCauseMatcher<T>(matcher); } }
@Test public void processErrorInChain() throws Exception { try { from(MessageProcessors.process(input, createChain(error))).block(); fail("Exception expected"); } catch (Throwable t) { assertThat(t.getCause(), is(instanceOf(MessagingException.class))); assertThat(t.getCause().getCause(), is(exception)); } assertThat(from(responsePublisher).toFuture().isDone(), is(true)); thrown.expectCause((is(instanceOf(MessagingException.class)))); thrown.expectCause(hasCause(is(exception))); from(responsePublisher).block(); }
/** * Returns a matcher that verifies that the outer exception has a cause for which the supplied matcher * evaluates to true. * * @param matcher to apply to the cause of the outer exception * @param <T> type of the outer exception */ @Factory public static <T extends Throwable> Matcher<T> hasCause(final Matcher<? extends Throwable> matcher) { return new ThrowableCauseMatcher<T>(matcher); } }
@Test public void processToApplyErrorInFlow() throws Exception { try { processToApply(input, createFlow(error)); fail("Exception expected"); } catch (Throwable t) { assertThat(t, is(instanceOf(MessagingException.class))); assertThat(t.getCause(), is(exception)); } assertThat(from(responsePublisher).toFuture().isDone(), is(true)); thrown.expectCause((is(instanceOf(MessagingException.class)))); thrown.expectCause(hasCause(is(exception))); from(responsePublisher).block(); }
@Test public void optimisticLockingViolationForConcurrentLikeModifications() throws InterruptedException, ExecutionException { expectedException.expectCause(new ThrowableCauseMatcher<>(IsInstanceOf.<Throwable>instanceOf(OptimisticLockException.class))); executeOperations( new IncrementLikes(), new IncrementLikes(), new ModifyTitle("JPA"), new ModifyViews(2L)); }
@Test public void processToApplyErrorInChain() throws Exception { try { processToApply(input, createChain(error)); fail("Exception expected"); } catch (Throwable t) { assertThat(t, is(instanceOf(MessagingException.class))); assertThat(t.getCause(), is(exception)); } assertThat(from(responsePublisher).toFuture().isDone(), is(true)); thrown.expectCause((is(instanceOf(MessagingException.class)))); thrown.expectCause(hasCause(is(exception))); from(responsePublisher).block(); }
@Test public void optimisticLockingViolationForConcurrentViewsModifications() throws InterruptedException, ExecutionException { expectedException.expectCause(new ThrowableCauseMatcher<>(IsInstanceOf.<Throwable>instanceOf(OptimisticLockException.class))); executeOperations( new IncrementLikes(), new ModifyViews(100L), new ModifyTitle("JPA"), new ModifyViews(1000L)); }
@Test public void testCancelTrueWithSleep() throws Exception { InterruptibleWithSleep interruptible = getInterruptibleWithSleep(); executor.execute(interruptible); interruptible.started.await(); interruptible.cancel(true); expectedException.expect(Matchers.<Throwable>either(instanceOf(CancellationException.class)) .or(Matchers.<Throwable>both(instanceOf(ExecutionException.class)) .and(ThrowableCauseMatcher.hasCause(instanceOf(InterruptedException.class))))); interruptible.get(); }
@Test public void optimisticLockingViolationForConcurrentPostModifications() throws InterruptedException, ExecutionException { expectedException.expectCause(new ThrowableCauseMatcher<>(IsInstanceOf.<Throwable>instanceOf(OptimisticLockException.class))); executeOperations( new IncrementLikes(), new ModifyTitle("Hibernate"), new ModifyTitle("JPA"), new ModifyViews(1L)); }