Search code examples
google-truth

How to negate assertions with google truth


A list of Integers can be tested if it is ordered:

assertThat(list).isOrdered();

but how can it be tested that it is not ordered?

Is there a way to write:

assertThat(list).not().isOrdered();

Or more generally is there a way to negate assertions with truth in a convenient manner?


Solution

  • Sorry, we don't have a way to perform negate assertions in general. Instead, we offer one-off methods like doesNotContain when we see demand.

    Part of the reason is that the code the runs during the "failure" path is fairly expensive, so users who run it a lot (for many successful tests) might be in for an unpleasant surprise. Another reason is that failure messages are written with the "positive" case in mind. For those reasons, my guess is that we're not going to offer this, but feel free to file a feature request -- or to file one for isNotOrdered() specifically. I don't think we've seen requests for that yet, so whatever information you can offer about your use case would be helpful. (In particular, we're used to seeing APIs that don't promise to return data in any particular order, but we're not used to seeing APIs that promise to return data out of order :))