Skip to content

Commit 65bf8b7

Browse files
author
joe.walnes
committed
Relaxed isInstanceOf() matcher generic type so it can be used on any kind of object.
e.g. assertThat(someUnknownObject, isInstanceOf(String.class)); (Previously that would not compile unless the object was casted to a String).
1 parent 78e3a91 commit 65bf8b7

5 files changed

Lines changed: 19 additions & 3 deletions

File tree

‎CHANGES.txt‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@
1717
* Created StringDescription.asString() alias (because toString() caused issues
1818
with static imports).
1919

20+
* Relaxed isInstanceOf() matcher generic type so it can be used on any kind of
21+
object. e.g. assertThat(someUnknownObject, isInstanceOf(String.class));
22+
2023
* TypeSafeMatcher.matchesSafely() is now public.
2124

2225
* Generator recognizes @Factory methods that return subclass of Matcher.

‎hamcrest-core/src/main/java/org/hamcrest/core/Is.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ public static <T> Matcher<T> is(T value) {
6161
* vs assertThat(cheese, is(Cheddar.class))
6262
*/
6363
@Factory
64-
public static <T> Matcher<T> is(Class<T> type) {
64+
public static Matcher<Object> is(Class type) {
6565
return is(instanceOf(type));
6666
}
6767

‎hamcrest-core/src/main/java/org/hamcrest/core/IsInstanceOf.java‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,14 @@ public void describeTo(Description description) {
3737
* Is the value an instance of a particular type?
3838
*/
3939
@Factory
40-
public static <T> Matcher<T> instanceOf(Class<T> type) {
40+
public static <T> Matcher<T> instanceOf2(Class<T> type) {
4141
return new IsInstanceOf<T>(type);
4242
}
4343

44+
@Factory
45+
@SuppressWarnings({"unchecked"})
46+
public static Matcher<Object> instanceOf(Class type) {
47+
return new IsInstanceOf<Object>(type);
48+
}
49+
4450
}

‎hamcrest-unit-test/src/main/java/org/hamcrest/core/IsInstanceOfTest.java‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ public void testEvaluatesToTrueIfArgumentIsInstanceOfASpecificClass() {
1818
assertThat(1, instanceOf(Number.class));
1919
assertThat(1.0, instanceOf(Number.class));
2020
assertThat(null, not(instanceOf(Number.class)));
21+
assertThat("hello", not(instanceOf(Number.class)));
2122
}
22-
23+
2324
}

‎hamcrest-unit-test/src/main/java/org/hamcrest/core/IsTest.java‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,10 @@ public void testProvidesConvenientShortcutForIsEqualTo() {
2929
assertDoesNotMatch("should not match", is("B"), "A");
3030
assertDescription("is \"A\"", is("A"));
3131
}
32+
33+
public void testProvidesConvenientShortcutForIsInstanceOf() {
34+
assertMatches("should match", is(String.class), "A");
35+
assertDoesNotMatch("should not match", is(Integer.class), "A");
36+
assertDoesNotMatch("should not match", is(Integer.class), null);
37+
}
3238
}

0 commit comments

Comments
 (0)