public class CatchThrowableHamcrestMatchers extends Object
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.
Hint: This class might use hamsandwich in the future but as long as hamsandwich is not in any public maven repository, this class will not use hamsandwich.
Constructor and Description |
---|
CatchThrowableHamcrestMatchers() |
Modifier and Type | Method and Description |
---|---|
static <T extends Throwable> |
hasMessage(String expectedMessage)
EXAMPLE:
|
static <T extends Throwable> |
hasMessageThat(org.hamcrest.Matcher<String> stringMatcher)
EXAMPLES:
|
static <T extends Throwable> |
hasNoCause()
EXAMPLE:
|
public CatchThrowableHamcrestMatchers()
public static <T extends Throwable> org.hamcrest.Matcher<T> hasMessage(String expectedMessage)
assertThat(caughtThrowable(), hasMessage("Index: 9, Size: 9"));
T
- the throwable subclassexpectedMessage
- the expected throwable messagepublic static <T extends Throwable> org.hamcrest.Matcher<T> hasMessageThat(org.hamcrest.Matcher<String> stringMatcher)
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
T
- the throwable subclassstringMatcher
- a string matcherpublic static <T extends Throwable> org.hamcrest.Matcher<T> hasNoCause()
assertThat(caughtThrowable(), hasNoCause());
T
- the throwable subclasscause
.Copyright © 2014. All rights reserved.