public class ExceptionMatcher extends org.hamcrest.TypeSafeDiagnosingMatcher<Procedure>
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 |
exception(Class<? extends Exception> exception)
This method behaves exactly the same as calling
throwsException(Class). |
static ExceptionMatcher |
throwsException()
Creates a matcher that matches if the examined procedure throws any exception.
|
static ExceptionMatcher |
throwsException(Class<? extends Exception> exception)
Creates a matcher that matches if the examined procedure throws a given exception.
|
static org.hamcrest.Matcher<Procedure> |
throwsNoException()
Creates a matcher that matches if the examined procedure throws no exception.
|
<T> ExceptionMatcher |
with(Function<? super T,Object> function,
org.hamcrest.Matcher<?> matcher)
Uses the given function to extract a value from the expected throwable, and a matcher to be
used against the function's result.
|
ExceptionMatcher |
withCause(Class<? extends Throwable> cause)
Assigns an expected cause for evaluation.
|
ExceptionMatcher |
withCause(ExceptionMatcher matcher)
Assigns another
ExceptionMatcher to be used for the exception cause validation. |
ExceptionMatcher |
withMessage(org.hamcrest.Matcher<String> matcher)
Assigns an external Matcher to be used in combination for the 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.
|
ExceptionMatcher |
withNoCause()
Instruct the matcher to ensure that the expected exception has no cause.
|
public static ExceptionMatcher throwsException()
For example:
assertThat(() -> obj.doStuff("p1"),
throwsException());
Note: This has the same effect as calling:
assertThat(() -> obj.doStuff("p1"),
throwsException(Exception.class));
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 Exception 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 static ExceptionMatcher exception(Class<? extends Exception> exception)
throwsException(Class).
It was created primarily to allow a nested ExceptionMatcher to validate the
cause of an exception.
For example:
assertThat(() -> obj.doStuff("p1"),
throwsException(IllegalArgumentException.class)
.withCause(
exception(FileNotFoundException.class)
.withMessage("File p1 not found"));
exception - the expected exception class. A null value is allowed, and means that
no exception is expectedthrowsException(Class)public static org.hamcrest.Matcher<Procedure> throwsNoException()
For example:
assertThat(() -> obj.doStuff("p1"),
throwsNoException());
Note: This has the same effect as calling:
assertThat(() -> obj.doStuff("p1"),
throwsException(null));
public 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 causepublic ExceptionMatcher withCause(ExceptionMatcher matcher)
ExceptionMatcher to be used for the exception cause validation.
For example:
assertThat(() -> obj.doStuff("p1"),
throwsException(IllegalArgumentException.class)
.withCause(
exception(FileNotFoundException.class)
.withMessage("File p1 not found"));
matcher - the matcher to be used in combination for exception cause validationexception(Class)public ExceptionMatcher withNoCause()
public 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 <T> ExceptionMatcher with(Function<? super T,Object> function, org.hamcrest.Matcher<?> matcher)
This can be particularly useful to extract custom exception data using a method reference or a lambda expression.
For example:
assertThat(() -> obj.doStuff(null), throwsException(MyCustomException.class) .with(MyCustomException::getErrorCode, equalTo(12001)) .with(MyCustomException::getLocalizedMessage, equalTo("Invalid parameter")));
T - the expected throwable typefunction - a function to be applied to extract the value from the throwablematcher - the matcher to be used against the function resultpublic void describeTo(org.hamcrest.Description description)
description - the Description to be appended toCopyright © 2024. All rights reserved.