How to assert that a list does not contain something, with FEST?
Asked Answered
A

2

5

I'm using FEST to write assertions in JUnit.

It's easy to assert that a list is containing some elements:

assertThat(list).contains(2,4);

But how to assert that the list is not containing something? Like:

assertThat(list).doesnotContain(3); // !!! no such method
Ataxia answered 15/5, 2014 at 14:37 Comment(0)
S
9

I've just been having a browse through the source code for the version 1 branch and found this:

/**
 * Verifies that the actual group of objects does not contain the given objects.
 *
 * @param objects the objects that the group of objects should exclude.
 * @return this assertion object.
 * @throws AssertionError       if the actual group of objects is {@code null}.
 * @throws NullPointerException if the given array is {@code null}.
 * @throws AssertionError       if the actual group of objects contains any of the given objects.
 */
public final @Nonnull S excludes(@Nonnull Object... objects) {
    assertExcludes(objects);
    return myself();
}

I probably shouldn't make wild assumptions like this, but it's in the same class as your contains method (ObjectGroupAssert) and the Javadoc seems to describe the functionality you're looking for.

So I think, you would just need:

assertThat(list).excludes(5,7);
Slippage answered 15/5, 2014 at 15:50 Comment(2)
It works! The method name excludes is a little surprise to meAtaxia
Huzzah! :) Yeah, excludes is new to me too, I'd've expected a doesNotContain variant. But thinking about it, I like excludes, but it might sit better with includes as it's counter-part rather than contains.Slippage
C
0

doesnotContain() method present in assertJ library. F.e.

assertThat(usersList).doesNotContain(userDTO);
Correlative answered 30/5, 2023 at 17:29 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.