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
Freewind
  • 193,756
  • 157
  • 432
  • 708

2 Answers2

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);
Dan Temple
  • 2,736
  • 2
  • 22
  • 39
  • It works! The method name `excludes` is a little surprise to me – Freewind May 15 '14 at 15:52
  • 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. – Dan Temple May 15 '14 at 15:57
0

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

assertThat(usersList).doesNotContain(userDTO);