Top Description Fields Constructors Methods
org.assertj.core.matcher

public abstract Class AssertionMatcher<T>

extends BaseMatcher<T>
Class Inheritance
Type Parameters
<T>
the type of the object to test
Imports
org.assertj.core.util.Throwables, org.hamcrest.BaseMatcher, .Description, .Matcher

Generic Hamcrest Matcher that reuses AssertJ assertions.

Overriding classes should only implement AssertionMatcher#assertion(Object) method as Matcher#matches(Object) and Matcher#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);
    }
  })
);
Author
Tomasz Kalkosiński
Since
2.7.0 / 3.7.0

Field Summary

Modifier and TypeField and Description
private AssertionError

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
public abstract void
assertion(T
assertion object
actual
)

Perform the assertions implemented in this method when the AssertionMatcher is used as an Hamcrest Matcher.

public void
describeTo(Description
The description to be built or appended to.
description
)

Implements org.hamcrest.SelfDescribing.describeTo.

Generates a description of the object.
public boolean
matches(Object
the object against which the matcher is evaluated.
argument
)

Implements org.hamcrest.Matcher.matches.

Evaluates the matcher for argument item.
Inherited from org.hamcrest.BaseMatcher:
_dont_implement_Matcher___instead_extend_BaseMatcher_describeMismatchisNotNulltoString

Field Detail

firstErrorback to summary
private AssertionError firstError

Constructor Detail

AssertionMatcherback to summary
public AssertionMatcher()

Method Detail

assertionback to summary
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:T

assertion object

Exceptions
AssertionError:
if the assertion object fails assertion
describeToback to summary
public void describeTo(Description description)

Implements org.hamcrest.SelfDescribing.describeTo.

Doc from org.hamcrest.SelfDescribing.describeTo.

Generates a description of the object. The description may be part of a a description of a larger object of which this is just a component, so it should be worded appropriately.

Parameters
description:Description

The description to be built or appended to.

Annotations
@Override
matchesback to summary
public boolean matches(Object argument)

Implements org.hamcrest.Matcher.matches.

Doc from org.hamcrest.Matcher.matches.

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
argument:Object

the object against which the matcher is evaluated.

Returns:boolean

true if item matches, otherwise false.

Annotations
@SuppressWarnings:unchecked
@Override