• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 package org.junit.matchers;
2 
3 import org.hamcrest.CoreMatchers;
4 import org.hamcrest.Matcher;
5 import org.hamcrest.core.CombinableMatcher.CombinableBothMatcher;
6 import org.hamcrest.core.CombinableMatcher.CombinableEitherMatcher;
7 import org.junit.internal.matchers.StacktracePrintingMatcher;
8 
9 /**
10  * Convenience import class: these are useful matchers for use with the assertThat method, but they are
11  * not currently included in the basic CoreMatchers class from hamcrest.
12  *
13  * @since 4.4
14  */
15 public class JUnitMatchers {
16     /**
17      * @return A matcher matching any collection containing element
18      * @deprecated Please use {@link CoreMatchers#hasItem(Object)} instead.
19      */
20     @Deprecated
hasItem(T element)21     public static <T> Matcher<Iterable<? super T>> hasItem(T element) {
22         return CoreMatchers.hasItem(element);
23     }
24 
25     /**
26      * @return A matcher matching any collection containing an element matching elementMatcher
27      * @deprecated Please use {@link CoreMatchers#hasItem(Matcher)} instead.
28      */
29     @Deprecated
hasItem(Matcher<? super T> elementMatcher)30     public static <T> Matcher<Iterable<? super T>> hasItem(Matcher<? super T> elementMatcher) {
31         return CoreMatchers.<T>hasItem(elementMatcher);
32     }
33 
34     /**
35      * @return A matcher matching any collection containing every element in elements
36      * @deprecated Please use {@link CoreMatchers#hasItems(Object...)} instead.
37      */
38     @Deprecated
hasItems(T... elements)39     public static <T> Matcher<Iterable<T>> hasItems(T... elements) {
40         return CoreMatchers.hasItems(elements);
41     }
42 
43     /**
44      * @return A matcher matching any collection containing at least one element that matches
45      *         each matcher in elementMatcher (this may be one element matching all matchers,
46      *         or different elements matching each matcher)
47      * @deprecated Please use {@link CoreMatchers#hasItems(Matcher...)} instead.
48      */
49     @Deprecated
hasItems(Matcher<? super T>.... elementMatchers)50     public static <T> Matcher<Iterable<T>> hasItems(Matcher<? super T>... elementMatchers) {
51         return CoreMatchers.<T>hasItems(elementMatchers);
52     }
53 
54     /**
55      * @return A matcher matching any collection in which every element matches elementMatcher
56      * @deprecated Please use {@link CoreMatchers#everyItem(Matcher)} instead.
57      */
58     @Deprecated
everyItem(final Matcher<T> elementMatcher)59     public static <T> Matcher<Iterable<T>> everyItem(final Matcher<T> elementMatcher) {
60         return CoreMatchers.everyItem((Matcher) elementMatcher);
61     }
62 
63     /**
64      * @return a matcher matching any string that contains substring
65      * @deprecated Please use {@link CoreMatchers#containsString(String)} instead.
66      */
67     @Deprecated
containsString(java.lang.String substring)68     public static Matcher<java.lang.String> containsString(java.lang.String substring) {
69         return CoreMatchers.containsString(substring);
70     }
71 
72     /**
73      * This is useful for fluently combining matchers that must both pass.  For example:
74      * <pre>
75      *   assertThat(string, both(containsString("a")).and(containsString("b")));
76      * </pre>
77      *
78      * @deprecated Please use {@link CoreMatchers#both(Matcher)} instead.
79      */
80     @Deprecated
both(Matcher<? super T> matcher)81     public static <T> CombinableBothMatcher<T> both(Matcher<? super T> matcher) {
82         return CoreMatchers.both(matcher);
83     }
84 
85     /**
86      * This is useful for fluently combining matchers where either may pass, for example:
87      * <pre>
88      *   assertThat(string, either(containsString("a")).or(containsString("b")));
89      * </pre>
90      *
91      * @deprecated Please use {@link CoreMatchers#either(Matcher)} instead.
92      */
93     @Deprecated
either(Matcher<? super T> matcher)94     public static <T> CombinableEitherMatcher<T> either(Matcher<? super T> matcher) {
95         return CoreMatchers.either(matcher);
96     }
97 
98     /**
99      * @return A matcher that delegates to throwableMatcher and in addition
100      *         appends the stacktrace of the actual Throwable in case of a mismatch.
101      */
isThrowable(Matcher<T> throwableMatcher)102     public static <T extends Throwable> Matcher<T> isThrowable(Matcher<T> throwableMatcher) {
103         return StacktracePrintingMatcher.isThrowable(throwableMatcher);
104     }
105 
106     /**
107      * @return A matcher that delegates to exceptionMatcher and in addition
108      *         appends the stacktrace of the actual Exception in case of a mismatch.
109      */
isException(Matcher<T> exceptionMatcher)110     public static <T extends Exception> Matcher<T> isException(Matcher<T> exceptionMatcher) {
111         return StacktracePrintingMatcher.isException(exceptionMatcher);
112     }
113 }
114