com.googlecode.catchexception.throwable.apis
Class CatchThrowableHamcrestMatchers

java.lang.Object
  extended by com.googlecode.catchexception.throwable.apis.CatchThrowableHamcrestMatchers

public class CatchThrowableHamcrestMatchers
extends Object

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.

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.

Since:
1.2.0
Author:
rwoo

Constructor Summary
CatchThrowableHamcrestMatchers()
           
 
Method Summary
static
<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>
hasNoCause()
          EXAMPLE:
assertThat(caughtThrowable(), hasNoCause());
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

CatchThrowableHamcrestMatchers

public CatchThrowableHamcrestMatchers()
Method Detail

hasMessage

public static <T extends Throwable> org.hamcrest.Matcher<T> hasMessage(String expectedMessage)
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

public static <T extends Throwable> org.hamcrest.Matcher<T> 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.


Copyright © 2015. All rights reserved.