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
Author:
Tomasz KalkosiƄski
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    abstract void
    assertion(T actual)
    Perform the assertions implemented in this method when the AssertionMatcher is used as an Hamcrest Matcher.
    void
    describeTo(org.hamcrest.Description description)
    boolean
    matches(Object argument)

    Methods inherited from class org.hamcrest.BaseMatcher

    _dont_implement_Matcher___instead_extend_BaseMatcher_, describeMismatch, isNotNull, toString

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
  • 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)