001 package org.junit.matchers; 002 003 import org.hamcrest.CoreMatchers; 004 import org.hamcrest.Matcher; 005 import org.hamcrest.core.CombinableMatcher.CombinableBothMatcher; 006 import org.hamcrest.core.CombinableMatcher.CombinableEitherMatcher; 007 import org.junit.internal.matchers.StacktracePrintingMatcher; 008 009 /** 010 * Convenience import class: these are useful matchers for use with the assertThat method, but they are 011 * not currently included in the basic CoreMatchers class from hamcrest. 012 * 013 * @since 4.4 014 * @deprecated use {@code org.hamcrest.junit.JUnitMatchers} 015 */ 016 @Deprecated 017 public class JUnitMatchers { 018 /** 019 * @return A matcher matching any collection containing element 020 * @deprecated Please use {@link CoreMatchers#hasItem(Object)} instead. 021 */ 022 @Deprecated 023 public static <T> Matcher<Iterable<? super T>> hasItem(T element) { 024 return CoreMatchers.hasItem(element); 025 } 026 027 /** 028 * @return A matcher matching any collection containing an element matching elementMatcher 029 * @deprecated Please use {@link CoreMatchers#hasItem(Matcher)} instead. 030 */ 031 @Deprecated 032 public static <T> Matcher<Iterable<? super T>> hasItem(Matcher<? super T> elementMatcher) { 033 return CoreMatchers.<T>hasItem(elementMatcher); 034 } 035 036 /** 037 * @return A matcher matching any collection containing every element in elements 038 * @deprecated Please use {@link CoreMatchers#hasItems(Object...)} instead. 039 */ 040 @Deprecated 041 public static <T> Matcher<Iterable<T>> hasItems(T... elements) { 042 return CoreMatchers.hasItems(elements); 043 } 044 045 /** 046 * @return A matcher matching any collection containing at least one element that matches 047 * each matcher in elementMatcher (this may be one element matching all matchers, 048 * or different elements matching each matcher) 049 * @deprecated Please use {@link CoreMatchers#hasItems(Matcher...)} instead. 050 */ 051 @Deprecated 052 public static <T> Matcher<Iterable<T>> hasItems(Matcher<? super T>... elementMatchers) { 053 return CoreMatchers.hasItems(elementMatchers); 054 } 055 056 /** 057 * @return A matcher matching any collection in which every element matches elementMatcher 058 * @deprecated Please use {@link CoreMatchers#everyItem(Matcher)} instead. 059 */ 060 @Deprecated 061 public static <T> Matcher<Iterable<T>> everyItem(final Matcher<T> elementMatcher) { 062 return CoreMatchers.everyItem(elementMatcher); 063 } 064 065 /** 066 * @return a matcher matching any string that contains substring 067 * @deprecated Please use {@link CoreMatchers#containsString(String)} instead. 068 */ 069 @Deprecated 070 public static Matcher<java.lang.String> containsString(java.lang.String substring) { 071 return CoreMatchers.containsString(substring); 072 } 073 074 /** 075 * This is useful for fluently combining matchers that must both pass. For example: 076 * <pre> 077 * assertThat(string, both(containsString("a")).and(containsString("b"))); 078 * </pre> 079 * 080 * @deprecated Please use {@link CoreMatchers#both(Matcher)} instead. 081 */ 082 @Deprecated 083 public static <T> CombinableBothMatcher<T> both(Matcher<? super T> matcher) { 084 return CoreMatchers.both(matcher); 085 } 086 087 /** 088 * This is useful for fluently combining matchers where either may pass, for example: 089 * <pre> 090 * assertThat(string, either(containsString("a")).or(containsString("b"))); 091 * </pre> 092 * 093 * @deprecated Please use {@link CoreMatchers#either(Matcher)} instead. 094 */ 095 @Deprecated 096 public static <T> CombinableEitherMatcher<T> either(Matcher<? super T> matcher) { 097 return CoreMatchers.either(matcher); 098 } 099 100 /** 101 * @return A matcher that delegates to throwableMatcher and in addition 102 * appends the stacktrace of the actual Throwable in case of a mismatch. 103 */ 104 public static <T extends Throwable> Matcher<T> isThrowable(Matcher<T> throwableMatcher) { 105 return StacktracePrintingMatcher.isThrowable(throwableMatcher); 106 } 107 108 /** 109 * @return A matcher that delegates to exceptionMatcher and in addition 110 * appends the stacktrace of the actual Exception in case of a mismatch. 111 */ 112 public static <T extends Exception> Matcher<T> isException(Matcher<T> exceptionMatcher) { 113 return StacktracePrintingMatcher.isException(exceptionMatcher); 114 } 115 }