Class IsInstanceOf

All Implemented Interfaces:
Matcher<Object>, SelfDescribing

public class IsInstanceOf extends DiagnosingMatcher<Object>
Tests whether the value is an instance of a class. Classes of basic types will be converted to the relevant "Object" classes
  • Field Details

    • expectedClass

      private final Class<?> expectedClass
    • matchableClass

      private final Class<?> matchableClass
  • Constructor Details

    • IsInstanceOf

      public IsInstanceOf(Class<?> expectedClass)
      Creates a new instance of IsInstanceOf
      Parameters:
      expectedClass - The predicate evaluates to true for instances of this class or one of its subclasses.
  • Method Details

    • matchableClass

      private static Class<?> matchableClass(Class<?> expectedClass)
    • matches

      protected boolean matches(Object item, Description mismatch)
      Specified by:
      matches in class DiagnosingMatcher<Object>
    • describeTo

      public void describeTo(Description description)
      Description copied from interface: SelfDescribing
      Generates a description of the object. The description may be part of a description of a larger object of which this is just a component, so it should be worded appropriately.
      Parameters:
      description - The description to be built or appended to.
    • instanceOf

      public static <T> Matcher<T> instanceOf(Class<?> type)
      Creates a matcher that matches when the examined object is an instance of the specified type, as determined by calling the Class.isInstance(Object) method on that type, passing the the examined object.

      The created matcher assumes no relationship between specified type and the examined object.

      For example:
      assertThat(new Canoe(), instanceOf(Paddlable.class));
      Type Parameters:
      T - the matcher type.
      Parameters:
      type - the type to check.
      Returns:
      The matcher.
    • any

      public static <T> Matcher<T> any(Class<T> type)
      Creates a matcher that matches when the examined object is an instance of the specified type, as determined by calling the Class.isInstance(Object) method on that type, passing the the examined object.

      The created matcher forces a relationship between specified type and the examined object, and should be used when it is necessary to make generics conform, for example in the JMock clause with(any(Thing.class))

      For example:
      assertThat(new Canoe(), instanceOf(Canoe.class));
      Type Parameters:
      T - the matcher type.
      Parameters:
      type - the type to check.
      Returns:
      The matcher.