Class 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 Summary

      Fields 
      Modifier and Type Field Description
      private java.lang.AssertionError firstError  
    • Method Summary

      All Methods Instance Methods Abstract Methods Concrete Methods 
      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​(java.lang.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
    • Field Detail

      • firstError

        private java.lang.AssertionError firstError
    • Constructor Detail

      • AssertionMatcher

        public AssertionMatcher()
    • Method Detail

      • matches

        public boolean matches​(java.lang.Object argument)
      • assertion

        public abstract void assertion​(T actual)
                                throws java.lang.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:
        java.lang.AssertionError - if the assertion object fails assertion
      • describeTo

        public void describeTo​(org.hamcrest.Description description)