public class CatchExceptionHamcrestMatchers extends Object
matchers
to match some
exception
properties.
EXAMPLE:
// given an empty list
List myList = new ArrayList();
// when we try to get the first element of the list
catchException(myList).get(1);
// then we expect an IndexOutOfBoundsException with message "Index: 1, Size: 0"
assertThat(caughtException(),
allOf(
is(IndexOutOfBoundsException.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 |
---|
CatchExceptionHamcrestMatchers() |
Modifier and Type | Method and Description |
---|---|
static <T extends Exception> |
hasMessage(String expectedMessage)
EXAMPLE:
|
static <T extends Exception> |
hasMessageThat(org.hamcrest.Matcher<String> stringMatcher)
EXAMPLES:
|
static <T extends Exception> |
hasNoCause()
EXAMPLE:
|
public CatchExceptionHamcrestMatchers()
public static <T extends Exception> org.hamcrest.Matcher<T> hasMessage(String expectedMessage)
assertThat(caughtException(), hasMessage("Index: 9, Size: 9"));
T
- the exception subclassexpectedMessage
- the expected exception messagepublic static <T extends Exception> org.hamcrest.Matcher<T> hasMessageThat(org.hamcrest.Matcher<String> stringMatcher)
assertThat(caughtException(), hasMessageThat(is("Index: 9, Size: 9")));
assertThat(caughtException(), hasMessageThat(containsString("Index: 9"))); // using JUnitMatchers
assertThat(caughtException(), hasMessageThat(containsPattern("Index: \\d+"))); // using Mockito's Find
T
- the exception subclassstringMatcher
- a string matcherpublic static <T extends Exception> org.hamcrest.Matcher<T> hasNoCause()
assertThat(caughtException(), hasNoCause());
T
- the exception subclasscause
.Copyright © 2014. All rights reserved.