com.googlecode.catchexception.apis.CatchExceptionHamcrestMatchers Maven / Gradle / Ivy
/**
* Copyright (C) 2011 [email protected]
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.googlecode.catchexception.apis;
import org.hamcrest.Matcher;
import org.junit.matchers.JUnitMatchers;
import com.googlecode.catchexception.apis.internal.hamcrest.ExceptionMessageMatcher;
import com.googlecode.catchexception.apis.internal.hamcrest.ExceptionNoCauseMatcher;
/**
* Provides some Hamcrest {@link Matcher matchers} to match some
* {@link Exception 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 {@link 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.
*
* @author rwoo
*/
public class CatchExceptionHamcrestMatchers {
/**
* EXAMPLE:
* assertThat(caughtException(), hasMessage("Index: 9, Size: 9"));
*
* @param
* the exception subclass
* @param expectedMessage
* the expected exception message
* @return Returns a matcher that matches an exception if it has the given
* message.
*/
public static org.hamcrest.Matcher hasMessage(
String expectedMessage) {
return new ExceptionMessageMatcher(expectedMessage);
}
/**
* EXAMPLES:
* 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
*
* @param
* the exception subclass
* @param stringMatcher
* a string matcher
* @return Returns a matcher that matches an exception if the given string
* matcher matches the exception message.
*/
public static org.hamcrest.Matcher hasMessageThat(
Matcher stringMatcher) {
return new ExceptionMessageMatcher(stringMatcher);
}
/**
* EXAMPLE:
* assertThat(caughtException(), hasNoCause());
*
* @param
* the exception subclass
* @return Returns a matcher that matches the exception if it does not have
* a {@link Throwable#getCause() cause}.
*/
public static org.hamcrest.Matcher hasNoCause() {
return new ExceptionNoCauseMatcher();
}
}