Class AssertionMatcher<T>

java.lang.Object
org.hamcrest.BaseMatcher<T>
org.assertj.core.matcher.AssertionMatcher<T>
Type Parameters:
T - the type of the object to test
All Implemented Interfaces:
org.hamcrest.Matcher<T>, org.hamcrest.SelfDescribing

public abstract class AssertionMatcher<T> extends org.hamcrest.BaseMatcher<T>
Generic Hamcrest Matcher that reuses AssertJ assertions.

Overriding classes should only implement assertion(Object) method as Matcher.matches(Object) and SelfDescribing.describeTo(Description) are provided.

If the matcher fails, the description will contain the stacktrace of the first failed assertion.

Example with Mockito:

 verify(customerRepository).save(argThat(new AssertionMatcher<Customer>() {
    @Override
    public void assertion(Customer actual) throws AssertionError {
      assertThat(actual).hasName("John")
                        .hasAge(30);
    }
  })
);
Since:
2.7.0 / 3.7.0
  • Field Details

  • Constructor Details

    • AssertionMatcher

      public AssertionMatcher()
  • Method Details

    • matches

      public boolean matches(Object argument)
    • assertion

      public abstract void assertion(T actual) throws AssertionError
      Perform the assertions implemented in this method when the AssertionMatcher is used as an Hamcrest Matcher. If the matcher fails, the description will contain the stacktrace of the first failed assertion.

      Example with Mockito:

       verify(customerRepository).save(argThat(new AssertionMatcher<Customer>() {
          @Override
          public void assertion(Customer actual) throws AssertionError {
            assertThat(actual).hasName("John")
                              .hasAge(30);
          }
        })
      );
      Parameters:
      actual - assertion object
      Throws:
      AssertionError - if the assertion object fails assertion
    • describeTo

      public void describeTo(org.hamcrest.Description description)