IsInstanceOf extends DiagnosingMatcher<Object> { @SuppressWarnings("unchecked") public static <T> Matcher<T> instanceOf(Class<?> type) { return (Matcher<T>) new IsInstanceOf(type); } IsInstanceOf(Class<?> expectedClass); @Override void describeTo(Description description); @SuppressWarnings("unchecked") static Matcher<T> instanceOf(Class<?> type); @SuppressWarnings("unchecked") static Matcher<T> any(Class<T> type);  }
@Test public void copesWithNullsAndUnknownTypes() { Matcher<?> matcher = instanceOf(Number.class); assertNullSafe(matcher); assertUnknownTypeSafe(matcher); }
@Test public void evaluatesToTrueIfArgumentIsInstanceOfASpecificClass() { final Matcher<Object> matcher = instanceOf(Number.class); assertMatches(matcher, 1); assertMatches(matcher, 1.1); assertDoesNotMatch(matcher, null); assertDoesNotMatch(matcher, new Object()); }
@Test public void hasAReadableDescription() { assertDescription("an instance of java.lang.Number", instanceOf(Number.class)); }
@Test public void describesActualClassInMismatchMessage() { assertMismatchDescription("\"some text\" is a java.lang.String", instanceOf(Number.class), "some text"); }
@Test public void instanceOfRequiresACastToReturnTheCorrectTypeForUseInJMock() { @SuppressWarnings("unused") Integer anInteger = (Integer)with(instanceOf(Integer.class)); }