Search code examples
assertj

Method anySatisfy from assertJ swallows actual failure message


I use assertJ in my project for nice formulation of test assertions. I call anySatisfy on a collection where I pass assertions in a lambda that must be met for at least one of the collection elements.

assertThat(myCollection).anySatisfy(myCollectionElement-> { 
    assertThat(myCollectionElement).callAnyAssertionMethod();
    assertThat(myCollectionElement).anotherAssertionMethod();
}

Once no element satisfies the required assertions, anySatisfy fails as expected.

The problem is that the console output then as follows

java.lang.AssertionError: 
Expecting any element of:
  <allCollectionElementsArSerializedHere>
to satisfy the given assertions requirements but none did.

at myPackage.myTestClass.myTestMethod(MyTestClass.java:xyz)

Concluding I do know that anySatisfy being called in line xyz failed, but I do not get to know which particular assertions inside the lambda are not met. I only can see that in the debugger.

How can I achieve to get the expected log output?


Solution

  • You can't but we have improved allSatisfy to report the unsatisfied requirements. We will do the same for anySatisfy for the next release, I have created https://github.com/joel-costigliola/assertj-core/issues/1400 to track this.