File tree Expand file tree Collapse file tree
hamcrest-core/src/main/java/org/hamcrest/core
hamcrest-unit-test/src/main/java/org/hamcrest/core Expand file tree Collapse file tree Original file line number Diff line number Diff line change 2020* Relaxed isInstanceOf() matcher generic type so it can be used on any kind of
2121 object. e.g. assertThat(someUnknownObject, isInstanceOf(String.class));
2222
23+ * Added any(Class<T>), null(Class<T>) and notNull(Class<T>) matchers, which returns
24+ Matcher<T>. Helpful when the compiler struggles with type inference.
25+
2326* Modified anyOf() and allOf() to accept mixed-types.
2427
2528* TypeSafeMatcher.matchesSafely() is now public.
Original file line number Diff line number Diff line change @@ -48,4 +48,12 @@ public static <T> Matcher<T> anything() {
4848 public static <T > Matcher <T > anything (String description ) {
4949 return new IsAnything <T >(description );
5050 }
51+
52+ /**
53+ * This matcher always evaluates to true. With type inference.
54+ */
55+ @ Factory
56+ public static <T > Matcher <T > any (@ SuppressWarnings ("unused" )Class <T > type ) {
57+ return new IsAnything <T >();
58+ }
5159}
Original file line number Diff line number Diff line change @@ -35,5 +35,21 @@ public static <T> Matcher<T> nullValue() {
3535 public static <T > Matcher <T > notNullValue () {
3636 return not (IsNull .<T >nullValue ());
3737 }
38+
39+ /**
40+ * Matches if value is null. With type inference.
41+ */
42+ @ Factory
43+ public static <T > Matcher <T > nullValue (@ SuppressWarnings ("unused" ) Class <T > type ) {
44+ return nullValue ();
45+ }
46+
47+ /**
48+ * Matches if value is not null. With type inference.
49+ */
50+ @ Factory
51+ public static <T > Matcher <T > notNullValue (@ SuppressWarnings ("unused" ) Class <T > type ) {
52+ return notNullValue ();
53+ }
3854}
3955
Original file line number Diff line number Diff line change 22 */
33package org .hamcrest .core ;
44
5+ import static org .hamcrest .core .IsAnything .any ;
56import org .hamcrest .AbstractMatcherTest ;
67import org .hamcrest .Matcher ;
78import static org .hamcrest .MatcherAssert .assertThat ;
@@ -27,4 +28,12 @@ public void testCanOverrideDescription() {
2728 String description = "description" ;
2829 assertDescription (description , anything (description ));
2930 }
31+
32+ public void testSupportsStaticTyping () {
33+ requiresStringMatcher (any (String .class ));
34+ }
35+
36+ private void requiresStringMatcher (Matcher <String > arg ) {
37+ // no-op
38+ }
3039}
Original file line number Diff line number Diff line change 88import static org .hamcrest .MatcherAssert .assertThat ;
99import static org .hamcrest .core .IsNull .nullValue ;
1010import static org .hamcrest .core .IsNull .notNullValue ;
11+ import static org .hamcrest .core .IsNull .*;
12+ import static org .hamcrest .core .IsAnything .any ;
1113
1214public class IsNullTest extends AbstractMatcherTest {
1315
@@ -22,4 +24,13 @@ public void testEvaluatesToTrueIfArgumentIsNull() {
2224 assertThat (ANY_NON_NULL_ARGUMENT , notNullValue ());
2325 assertThat (null , not (notNullValue ()));
2426 }
27+
28+ public void testSupportsStaticTyping () {
29+ requiresStringMatcher (nullValue (String .class ));
30+ requiresStringMatcher (notNullValue (String .class ));
31+ }
32+
33+ private void requiresStringMatcher (Matcher <String > arg ) {
34+ // no-op
35+ }
2536}
You can’t perform that action at this time.
0 commit comments