parent
d3b713d5f0
commit
66eb86f055
1 changed files with 0 additions and 66 deletions
@ -1,66 +0,0 @@ |
|||||||
/* |
|
||||||
* Copyright 2002-2019 the original author or authors. |
|
||||||
* |
|
||||||
* 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 |
|
||||||
* |
|
||||||
* https://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 org.springframework.tests; |
|
||||||
|
|
||||||
import org.hamcrest.BaseMatcher; |
|
||||||
import org.hamcrest.Description; |
|
||||||
import org.hamcrest.Matcher; |
|
||||||
import org.junit.Rule; |
|
||||||
|
|
||||||
/** |
|
||||||
* Additional hamcrest matchers. |
|
||||||
* |
|
||||||
* @author Phillip Webb |
|
||||||
*/ |
|
||||||
public class Matchers { |
|
||||||
|
|
||||||
/** |
|
||||||
* Create a matcher that wrapps the specified matcher and tests against the |
|
||||||
* {@link Throwable#getCause() cause} of an exception. If the item tested |
|
||||||
* is {@code null} not a {@link Throwable} the wrapped matcher will be called |
|
||||||
* with a {@code null} item. |
|
||||||
* |
|
||||||
* <p>Often useful when working with JUnit {@link ExpectedException} |
|
||||||
* {@link Rule @Rule}s, for example: |
|
||||||
* <pre> |
|
||||||
* thrown.expect(DataAccessException.class); |
|
||||||
* thrown.except(exceptionCause(isA(SQLException.class))); |
|
||||||
* </pre> |
|
||||||
* |
|
||||||
* @param matcher the matcher to wrap (must not be null) |
|
||||||
* @return a matcher that tests using the exception cause |
|
||||||
*/ |
|
||||||
@SuppressWarnings("unchecked") |
|
||||||
public static <T> Matcher<T> exceptionCause(final Matcher<T> matcher) { |
|
||||||
return (Matcher<T>) new BaseMatcher<Object>() { |
|
||||||
@Override |
|
||||||
public boolean matches(Object item) { |
|
||||||
Throwable cause = null; |
|
||||||
if (item != null && item instanceof Throwable) { |
|
||||||
cause = ((Throwable)item).getCause(); |
|
||||||
} |
|
||||||
return matcher.matches(cause); |
|
||||||
} |
|
||||||
|
|
||||||
@Override |
|
||||||
public void describeTo(Description description) { |
|
||||||
description.appendText("exception cause ").appendDescriptionOf(matcher); |
|
||||||
} |
|
||||||
}; |
|
||||||
} |
|
||||||
|
|
||||||
} |
|
Loading…
Reference in new issue