Class DescribedAs<T>

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

public class DescribedAs<T> extends BaseMatcher<T>
Provides a custom description to another matcher.
  • Constructor Details

    • DescribedAs

      public DescribedAs(String descriptionTemplate, Matcher<T> matcher, Object[] values)
      Constructor, best called from describedAs(String, Matcher, Object...).
      Parameters:
      descriptionTemplate - the new description for the wrapped matcher
      matcher - the matcher to wrap
      values - optional values to insert into the tokenised description
  • Method Details

    • matches

      public boolean matches(Object o)
      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:
      o - 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 description)
      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.
      description - The description to be built or appended to.
    • describedAs

      public static <T> Matcher<T> describedAs(String descriptionTemplate, Matcher<T> matcher, Object... values)
      Wraps an existing matcher, overriding its description with that specified. All other functions are delegated to the decorated matcher, including its mismatch description. For example:
      describedAs("a big decimal equal to %0", equalTo(myBigDecimal), myBigDecimal.toPlainString())
      Type Parameters:
      T - the matcher type.
      Parameters:
      descriptionTemplate - the new description for the wrapped matcher
      matcher - the matcher to wrap
      values - optional values to insert into the tokenised description
      Returns:
      The matcher.