public class ExceptionMatchers extends Object
| Modifier and Type | Class and Description |
|---|---|
static class |
ExceptionMatchers.CatchResult
Holder for the exception thrown by a
ExceptionMatchers.ThrowingRunnable. |
static interface |
ExceptionMatchers.ThrowingRunnable
Like
Runnable, but permits checked exceptions to be thrown. |
| Constructor and Description |
|---|
ExceptionMatchers() |
| Modifier and Type | Method and Description |
|---|---|
static void |
assertThrows(Class<? extends Throwable> expected,
ExceptionMatchers.ThrowingRunnable runnable)
|
static void |
assertThrows(org.hamcrest.Matcher<Throwable> matcher,
ExceptionMatchers.ThrowingRunnable runnable)
|
static void |
assertThrows(String message,
Class<? extends Throwable> expected,
ExceptionMatchers.ThrowingRunnable runnable)
Convenience method for
assertThrows(String, Matcher, ThrowingRunnable) with instanceOf(expected)
for the matcher. |
static void |
assertThrows(String message,
org.hamcrest.Matcher<Throwable> matcher,
ExceptionMatchers.ThrowingRunnable runnable)
This is similar to using the
expected parameter on a @Test annotation or
the ExpectedException @Rule, except that it can be used conveniently within the test flow. |
static ExceptionMatchers.CatchResult |
catching(ExceptionMatchers.ThrowingRunnable runnable)
Runs the
ExceptionMatchers.ThrowingRunnable, catches any exception that it throws, and returns
it wrapped up as a CatchResult that can be asserted against with caught(Matcher) or
caughtNothing(). |
static org.hamcrest.Matcher<ExceptionMatchers.CatchResult> |
caught(Class<? extends Throwable> expected)
A matcher for use with
catching(ThrowingRunnable) which asserts that it throws an exception of the
specified type. |
static org.hamcrest.Matcher<ExceptionMatchers.CatchResult> |
caught(org.hamcrest.Matcher<Throwable> matcher)
A matcher for use with
catching(ThrowingRunnable) which asserts that it throws an exception that
matches the given condition. |
static org.hamcrest.Matcher<ExceptionMatchers.CatchResult> |
caughtNothing()
A matcher for use with
catching(ThrowingRunnable) which asserts that no exception occurred. |
static NamedFeature<Throwable,Throwable> |
cause()
Extracts an exception's cause for matching against.
|
static org.hamcrest.Matcher<Throwable> |
cause(Class<? extends Throwable> expectedClass)
|
static org.hamcrest.Matcher<Throwable> |
cause(org.hamcrest.Matcher<Throwable> matcher)
|
static NamedFeature<Throwable,String> |
message()
Extracts an exception's message for matching against.
|
static org.hamcrest.Matcher<Throwable> |
message(org.hamcrest.Matcher<String> matcher)
|
static org.hamcrest.Matcher<Throwable> |
message(String message)
Convenient shorthand for
. |
static org.hamcrest.Matcher<Throwable> |
messageContains(String... orderedSubstrings)
Convenient shorthand for
. |
public static void assertThrows(Class<? extends Throwable> expected, ExceptionMatchers.ThrowingRunnable runnable)
expected - as for assertThrows(String, Class, ThrowingRunnable)runnable - as for assertThrows(String, Matcher, ThrowingRunnable)public static void assertThrows(String message, Class<? extends Throwable> expected, ExceptionMatchers.ThrowingRunnable runnable)
assertThrows(String, Matcher, ThrowingRunnable) with instanceOf(expected)
for the matcher.expected - as for assertThrows(String, Class, ThrowingRunnable)runnable - as for assertThrows(String, Matcher, ThrowingRunnable)public static void assertThrows(org.hamcrest.Matcher<Throwable> matcher, ExceptionMatchers.ThrowingRunnable runnable)
matcher - as for assertThrows(String, Matcher, ThrowingRunnable)runnable - as for assertThrows(String, Matcher, ThrowingRunnable)public static void assertThrows(@Nullable String message, @Nonnull org.hamcrest.Matcher<Throwable> matcher, @Nonnull ExceptionMatchers.ThrowingRunnable runnable)
expected parameter on a @Test annotation or
the ExpectedException @Rule, except that it can be used conveniently within the test flow.
Without assertThrows:
@Test
public void shouldThrowExceptionForBadInput() {
WidgetFactory widgetFactory = new WidgetFactory();
try {
widgetFactory.lookup("foo");
fail("Did not expect 'foo' to be a valid widget");
} catch (IllegalArgumentException e) {
assertThat(e, message(containsString("foo")));
assertThat(widgetFactory.stats().lookupCount(), is(1));
}
}
With assertThrows:
@Test
public void shouldThrowExceptionForBadInput() {
WidgetFactory widgetFactory = new WidgetFactory();
assertThrows(allOf(instanceOf(IllegalArgumentException.class), message(containsString("foo"))),
() -> widgetFactory.lookup("foo"));
assertThat(widgetFactory.stats().lookupCount(), is(1));
}
message - an optional message clarifying why the exception was expectedmatcher - a matcher to verify that the exception meets expectationsrunnable - the code that is expected to throw an exceptionpublic static NamedFeature<Throwable,String> message()
This is the same thing as calling Throwable.getMessage() and matching against that directly,
except that it is a NamedFeature, so failures report more detailed information about mismatches,
including what the exception class was.
public static org.hamcrest.Matcher<Throwable> message(String message)
message().NamedFeature.thatIs(Object) thatIs}(message).message - the full text of the exception's expected messagepublic static org.hamcrest.Matcher<Throwable> message(org.hamcrest.Matcher<String> matcher)
matcher - the matcher to apply to the exception's messagepublic static org.hamcrest.Matcher<Throwable> messageContains(String... orderedSubstrings)
message().that(orderedSubstrings(orderedSubstrings)).orderedSubstrings - one or more substrings expected to be found in the exception's message, in the
order in which they must be foundpublic static NamedFeature<Throwable,Throwable> cause()
This is the same thing as calling Throwable.getCause() and matching against that directly,
except that it is a NamedFeature, so failures report more detailed information about mismatches,
including what the original exception was.
public static org.hamcrest.Matcher<Throwable> cause(Class<? extends Throwable> expectedClass)
expectedClass - the expected type of the exception's causepublic static org.hamcrest.Matcher<Throwable> cause(org.hamcrest.Matcher<Throwable> matcher)
matcher - the matcher to apply to the exception's causepublic static ExceptionMatchers.CatchResult catching(ExceptionMatchers.ThrowingRunnable runnable)
ExceptionMatchers.ThrowingRunnable, catches any exception that it throws, and returns
it wrapped up as a CatchResult that can be asserted against with caught(Matcher) or
caughtNothing().runnable - the runnable code that might throw an exceptionempty()public static org.hamcrest.Matcher<ExceptionMatchers.CatchResult> caughtNothing()
catching(ThrowingRunnable) which asserts that no exception occurred.
Of course, you could just let the exception escape instead, but there may be cases where this makes a test more convenient to write, so it is included for completeness.
assertThat(catching(Thread::yield), caughtNothing());
CatchResult where nothing was thrownpublic static org.hamcrest.Matcher<ExceptionMatchers.CatchResult> caught(@Nonnull Class<? extends Throwable> expected)
catching(ThrowingRunnable) which asserts that it throws an exception of the
specified type.expected - the type of exception that is expected to be thrownpublic static org.hamcrest.Matcher<ExceptionMatchers.CatchResult> caught(@Nonnull org.hamcrest.Matcher<Throwable> matcher)
catching(ThrowingRunnable) which asserts that it throws an exception that
matches the given condition.matcher - the matcher that the thrown exception is expected to satisfyCopyright © 2017 Atlassian. All rights reserved.