How do I negate assertions in AssertJ?

It is not possible to combine normal assertions methods, this is an area where Hamcrest is more flexible than AssertJ.

In your case I would write a Condition as you suggested or use a lambda with matches assertion.


This will do the trick:

assertThat(Collections.singleton("1234xxx5678")).noneSatisfy(candidate -> 
    assertThat(candidate).containsOnlyDigits()
);

Slightly hacky, and the clarity of intention is not quite up to normal assertJ standards, but for the completely general case of negating any assertion it is what we have so far.

A fluent not() would be confusing when combined with chained asserts, but I do wish we could have a general doesNotSatisfy(Consumer) on AbstractObjectAssert

Tags:

Assertj