Class CatchThrowableHamcrestMatchers
java.lang.Object
com.googlecode.catchexception.throwable.apis.CatchThrowableHamcrestMatchers
Provides some Hamcrest
matchers
to match some throwable
properties.
EXAMPLE: // given an empty list
List myList = new ArrayList();
// when we try to get the first element of the list
catchThrowable(myList).get(1);
// then we expect an IndexOutOfBoundsThrowable with message "Index: 1, Size: 0"
assertThat(caughtThrowable(),
allOf(
is(IndexOutOfBoundsThrowable.class),
hasMessage("Index: 1, Size: 0"),
hasNoCause()
)
);
To combine the standard Hamcrest matchers, your custom matchers, these matchers, and other matcher collections (as JUnitMatchers in a single class follow the instructions outlined in Sugar generation.
-
Method Summary
Modifier and TypeMethodDescriptionstatic <T extends Throwable>
org.hamcrest.Matcher<T> hasMessage
(String expectedMessage) EXAMPLE:assertThat(caughtThrowable(), hasMessage("Index: 9, Size: 9"));
.static <T extends Throwable>
org.hamcrest.Matcher<T> hasMessageThat
(org.hamcrest.Matcher<String> stringMatcher) EXAMPLES:assertThat(caughtThrowable(), hasMessageThat(is("Index: 9, Size: 9"))); assertThat(caughtThrowable(), hasMessageThat(containsString("Index: 9"))); // using JUnitMatchers assertThat(caughtThrowable(), hasMessageThat(containsPattern("Index: \\d+"))); // using Mockito's Find
.static <T extends Throwable>
org.hamcrest.Matcher<T> EXAMPLE:assertThat(caughtThrowable(), hasNoCause());
.
-
Method Details
-
hasMessage
EXAMPLE:assertThat(caughtThrowable(), hasMessage("Index: 9, Size: 9"));
.- Type Parameters:
T
- the throwable subclass- Parameters:
expectedMessage
- the expected throwable message- Returns:
- Returns a matcher that matches an throwable if it has the given message.
-
hasMessageThat
public static <T extends Throwable> org.hamcrest.Matcher<T> hasMessageThat(org.hamcrest.Matcher<String> stringMatcher) EXAMPLES:assertThat(caughtThrowable(), hasMessageThat(is("Index: 9, Size: 9"))); assertThat(caughtThrowable(), hasMessageThat(containsString("Index: 9"))); // using JUnitMatchers assertThat(caughtThrowable(), hasMessageThat(containsPattern("Index: \\d+"))); // using Mockito's Find
.- Type Parameters:
T
- the throwable subclass- Parameters:
stringMatcher
- a string matcher- Returns:
- Returns a matcher that matches an throwable if the given string matcher matches the throwable message.
-
hasNoCause
EXAMPLE:assertThat(caughtThrowable(), hasNoCause());
.- Type Parameters:
T
- the throwable subclass- Returns:
- Returns a matcher that matches the throwable if it does not have a
cause
.
-