/** * Adds {@code matcher} to the list of requirements for any thrown * exception. */ public void expect(Matcher<?> matcher) { fMatcherBuilder.add(matcher); }
Matcher<Throwable> build() { return isThrowable(allOfTheMatchers()); }
private void handleException(Throwable e) throws Throwable { if (fMatcherBuilder.expectsThrowable()) { assertThat(e, fMatcherBuilder.build()); } else { throw e; } } }
private Matcher<Throwable> allOfTheMatchers() { if (matchers.size() == 1) { return cast(matchers.get(0)); } return allOf(castedMatchers()); }
private String missingExceptionMessage() { String expectation= StringDescription.toString(matcherBuilder.build()); return format(missingExceptionMessage, expectation); } }
/** * Check if any Exception is expected. * @since 4.13 */ public final boolean isAnyExceptionExpected() { return matcherBuilder.expectsThrowable(); }
private Matcher<Throwable> allOfTheMatchers() { if (fMatchers.size() == 1) { return cast(fMatchers.get(0)); } return allOf(castedMatchers()); }
private void handleException(Throwable e) throws Throwable { if (isAnyExceptionExpected()) { assertThat(e, matcherBuilder.build()); } else { throw e; } }
@Override public void evaluate() throws Throwable { try { fNext.evaluate(); if (fMatcherBuilder.expectsThrowable()) { failDueToMissingException(); } } catch (AssumptionViolatedException e) { optionallyHandleException(e, handleAssumptionViolatedExceptions); } catch (AssertionError e) { optionallyHandleException(e, handleAssertionErrors); } catch (Throwable e) { handleException(e); } } }
private Matcher<Throwable> allOfTheMatchers() { if (fMatchers.size() == 1) { return cast(fMatchers.get(0)); } return allOf(castedMatchers()); }
private void handleException(Throwable e) throws Throwable { if (fMatcherBuilder.expectsThrowable()) { assertThat(e, fMatcherBuilder.build()); } else { throw e; } } }
private void failDueToMissingException() throws AssertionError { String expectation = StringDescription.toString(fMatcherBuilder.build()); fail("Expected test to throw " + expectation); }
@Override public void evaluate() throws Throwable { try { fNext.evaluate(); if (fMatcherBuilder.expectsThrowable()) { failDueToMissingException(); } } catch (AssumptionViolatedException e) { optionallyHandleException(e, handleAssumptionViolatedExceptions); } catch (AssertionError e) { optionallyHandleException(e, handleAssertionErrors); } catch (Throwable e) { handleException(e); } } }
/** * Verify that your code throws an exception that is matched by * a Hamcrest matcher. * <pre> @Test * public void throwsExceptionThatCompliesWithMatcher() { * NullPointerException e = new NullPointerException(); * thrown.expect(is(e)); * throw e; * }</pre> * * @deprecated use {@code org.hamcrest.junit.ExpectedException.expect()} */ @Deprecated public void expect(Matcher<?> matcher) { matcherBuilder.add(matcher); }
Matcher<Throwable> build() { return isThrowable(allOfTheMatchers()); }
private Matcher<Throwable> allOfTheMatchers() { if (fMatchers.size() == 1) { return cast(fMatchers.get(0)); } return allOf(castedMatchers()); }
private void handleException(Throwable e) throws Throwable { if (fMatcherBuilder.expectsThrowable()) { assertThat(e, fMatcherBuilder.build()); } else { throw e; } } }
private void failDueToMissingException() throws AssertionError { String expectation = StringDescription.toString(fMatcherBuilder.build()); fail("Expected test to throw " + expectation); }
private boolean isAnyExceptionExpected() { return matcherBuilder.expectsThrowable(); }
/** * Adds {@code matcher} to the list of requirements for any thrown * exception. */ public void expect(Matcher<?> matcher) { fMatcherBuilder.add(matcher); }