public class ExceptionMatcher extends org.hamcrest.TypeSafeDiagnosingMatcher<Runnable>
For example:
assertThat(() -> obj.doStuff("p1"),
throwsException(NullPointerException.class));
Exception details, such as message and cause, can also be evaluated using additional methods. For example:
assertThat(() -> obj.doStuff("p1"),
throwsException(IllegalStateException.class)
.withCause(FileNotFoundException.class));
assertThat(() -> obj.doStuff(null),
throwsException(MyException.class)
.withMessageContaining("ERR-12008"));
| Modifier and Type | Method and Description |
|---|---|
void |
describeTo(org.hamcrest.Description description)
Describes the "expected" pat of the test description.
|
static ExceptionMatcher |
throwsException(Class<? extends Exception> exception)
Creates a matcher that matches if the examined procedure throws a given exception.
|
ExceptionMatcher |
withCause(Class<? extends Throwable> cause)
Assigns an expected cause for evaluation.
|
ExceptionMatcher |
withMessage(org.hamcrest.Matcher<String> matcher)
Assigns an external Matcher to be used in combination for exception message validation.
|
ExceptionMatcher |
withMessage(String message)
Assigns an expected message for validation.
|
ExceptionMatcher |
withMessageContaining(String... substrings)
Assigns one or more expected substrings for exception message validation.
|
public static ExceptionMatcher throwsException(Class<? extends Exception> exception)
For example:
assertThat(() -> obj.doStuff("p1"),
throwsException(IllegalStateException.class));
The matcher matches if the actual exception class is either the same as, or is a child
of, the Throwable represented by the specified parameter.
For example, if the examined code throws a NullPointerException, all of the
following assertions are valid:
assertThat( ... throwsException(NullPointerException.class));
assertThat( ... throwsException(RuntimeException.class));
assertThat( ... throwsException(Exception.class));
In other words, the matcher tests whether the actual exception can be converted to the specified class.
exception - the expected exception class. A null value is allowed, and means that
no exception is expectedpublic ExceptionMatcher withCause(Class<? extends Throwable> cause)
For example:
assertThat(() -> obj.doStuff("p1"),
throwsException(IllegalStateException.class)
.withCause(FileNotFoundException.class));
The matcher matches if the actual cause class is either the same as, or is a child of,
the Throwable represented by the specified parameter.
For example, if the examined cause is a NullPointerException, all of the
following assertions are valid:
withCause(NullPointerException.class);
withCause(RuntimeException.class);
withCause(Exception.class);
In other words, the matcher tests whether the actual cause can be converted to the specified class.
cause - the expected cause. A null value is allowed, and means that an exception
without cause is expectedpublic ExceptionMatcher withMessage(org.hamcrest.Matcher<String> matcher)
For example:
CoreMatchers:
assertThat(() -> obj.doStuff(null),
throwsException(IllegalArgumentException.class)
.withMessage(equalTo("argument cannot be null")));
CoreMatchers and StringMatcher:
assertThat(() -> obj.doStuff(null),
throwsException(MyException.class)
.withMessage(either(startsWith("ERR-0001"))
.or(containsAny("division by zero").ignoreCase())));
matcher - the matcher to be used in combination for exception message validationpublic ExceptionMatcher withMessage(String message)
For example:
assertThat(() -> obj.doStuff(null),
throwsException(IllegalArgumentException.class)
.withMessage("argument cannot be null"));
Note: This has the same effect as calling:
withMessage(equalTo("argument cannot be null"));
message - the message for exception validationpublic ExceptionMatcher withMessageContaining(String... substrings)
For example:
assertThat(() -> obj.doStuff(null),
throwsException(IllegalArgumentException.class)
.withMessageContaining("argument cannot be null"));
assertThat(() -> obj.doStuff(null),
throwsException(MyException.class)
.withMessageContaining("ERR-12008", "mandatory"));
substrings - one or more substrings for exception message validationpublic void describeTo(org.hamcrest.Description description)
SelfDescribing.describeTo(Description)Copyright © 2020. All rights reserved.