CausedBy extends TypesafeNestedResultMatcher<Throwable> { public CausedBy(boolean direct, Matcher<? super Throwable> m) { super(Throwable.class); this.direct = direct; this.throwableMatcher = m; } CausedBy(boolean direct, Matcher<? super Throwable> m); @Override int getDescriptionPrecedence(); @Override void describeTo(Description description); @Factory static CausedBy causedBy(Matcher<? super Throwable> matcher); @Factory static CausedBy causedBy(Class<? extends Throwable> clazz); @Factory static CausedBy causedBy(String message); @Factory static CausedBy causedBy(Class<? extends Throwable> clazz, String message); @Factory static CausedBy causedBy(Class<? extends Throwable> clazz, Matcher<? super Throwable> matcher); @Factory static CausedBy causedBy(Class<? extends Throwable> clazz, String message, Matcher<? super Throwable> matcher); @Factory static CausedBy directlyCausedBy(Matcher<? super Throwable> matcher); @Factory static CausedBy directlyCausedBy(Class<? extends Throwable> clazz); @Factory static CausedBy directlyCausedBy(Class<? extends Throwable> clazz, String message); @Factory static CausedBy directlyCausedBy(Class<? extends Throwable> clazz, Matcher<? super Throwable> matcher); @Factory static CausedBy directlyCausedBy(Class<? extends Throwable> clazz, String message, Matcher<? super Throwable> matcher);  }
@Test public void test_syntax() { RuntimeException r = new RuntimeException("a", new RuntimeException("b")); assertThat(r, causedBy(RuntimeException.class)); Object o = r; assertThat(o, is(throwable(causedBy(RuntimeException.class)))); }
@Test public void test_mismatch_description() { final Matcher<Throwable> hasCause = causedBy(messageContains("x")); final Throwable t = new RuntimeException("a", new RuntimeException("b", new RuntimeException("c"))); assertThat("Matching fails", hasCause.matches(t), is(false)); StringDescription desc = new StringDescription(); hasCause.describeMismatch(t, desc); assertThat(desc.toString(), is("cause 1 message \"b\" did not contain /x/, " + "cause 2 message \"c\" did not contain /x/")); }