001/* 002 * Copyright 2002-2015 the original author or authors. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * https://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 017package org.springframework.test.util; 018 019import org.hamcrest.Description; 020import org.hamcrest.Matcher; 021import org.hamcrest.StringDescription; 022 023/** 024 * A replacement of {@link org.hamcrest.MatcherAssert} that removes the need to 025 * depend on "hamcrest-all" when using Hamcrest 1.1 and also maintains backward 026 * compatibility with Hamcrest 1.1 (also embedded in JUnit 4.4 through 4.8). 027 * 028 * @author Rossen Stoyanchev 029 * @author Sam Brannen 030 * @since 3.2 031 * @deprecated as of Spring 4.2, in favor of the original 032 * {@link org.hamcrest.MatcherAssert} class with JUnit 4.9 / Hamcrest 1.3 033 */ 034@Deprecated 035public abstract class MatcherAssertionErrors { 036 037 /** 038 * Assert that the given matcher matches the actual value. 039 * @param <T> the static type accepted by the matcher 040 * @param actual the value to match against 041 * @param matcher the matcher 042 */ 043 public static <T> void assertThat(T actual, Matcher<T> matcher) { 044 assertThat("", actual, matcher); 045 } 046 047 /** 048 * Assert that the given matcher matches the actual value. 049 * @param <T> the static type accepted by the matcher 050 * @param reason additional information about the error 051 * @param actual the value to match against 052 * @param matcher the matcher 053 */ 054 public static <T> void assertThat(String reason, T actual, Matcher<T> matcher) { 055 if (!matcher.matches(actual)) { 056 Description description = new StringDescription(); 057 description.appendText(reason); 058 description.appendText("\nExpected: "); 059 description.appendDescriptionOf(matcher); 060 description.appendText("\n but: "); 061 matcher.describeMismatch(actual, description); 062 throw new AssertionError(description.toString()); 063 } 064 } 065 066}