Class Is<T>

java.lang.Object
org.hamcrest.BaseMatcher<T>
org.hamcrest.core.Is<T>
Type Parameters:
T - the matched value type
All Implemented Interfaces:
Matcher<T>, SelfDescribing

public class Is<T> extends BaseMatcher<T>
Decorates another Matcher, retaining the behaviour but allowing tests to be slightly more expressive. For example: assertThat(cheese, equalTo(smelly)) vs. assertThat(cheese, is(equalTo(smelly)))
  • Constructor Details

  • Method Details

    • matches

      public boolean matches(Object arg)
      Description copied from interface: Matcher
      Evaluates the matcher for argument item. This method matches against Object, instead of the generic type T. This is because the caller of the Matcher does not know at runtime what the type is (because of type erasure with Java generics). It is down to the implementations to check the correct type.
      Parameters:
      arg - the object against which the matcher is evaluated.
      Returns:
      true if item matches, otherwise false.
      See Also:
    • 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.
    • describeMismatch

      public void describeMismatch(Object item, Description mismatchDescription)
      Description copied from interface: Matcher
      Generate a description of why the matcher has not accepted the item. The description will be part of a larger description of why a matching failed, so it should be concise. This method assumes that matches(item) is false, but will not check this.
      Specified by:
      describeMismatch in interface Matcher<T>
      Overrides:
      describeMismatch in class BaseMatcher<T>
      Parameters:
      item - The item that the Matcher has rejected.
      mismatchDescription - The description to be built or appended to.
    • is

      public static <T> Matcher<T> is(Matcher<T> matcher)
      Decorates another Matcher, retaining its behaviour, but allowing tests to be slightly more expressive. For example:
      assertThat(cheese, is(equalTo(smelly)))
      instead of:
      assertThat(cheese, equalTo(smelly))
      Type Parameters:
      T - the matcher type.
      Parameters:
      matcher - the matcher to wrap.
      Returns:
      The matcher.
    • is

      public static <T> Matcher<T> is(T value)
      A shortcut to the frequently used is(equalTo(x)). For example:
      assertThat(cheese, is(smelly))
      instead of:
      assertThat(cheese, is(equalTo(smelly)))
      Type Parameters:
      T - the matcher type.
      Parameters:
      value - the value to check.
      Returns:
      The matcher.
    • isA

      public static <T> Matcher<T> isA(Class<?> type)
      A shortcut to the frequently used is(instanceOf(SomeClass.class)). For example:
      assertThat(cheese, isA(Cheddar.class))
      instead of:
      assertThat(cheese, is(instanceOf(Cheddar.class)))
      Type Parameters:
      T - the matcher type.
      Parameters:
      type - the type to check.
      Returns:
      The matcher.