Is it possible to assert there are only duplicates with an AbstractIterableAssert?

Viewed 29

I have something like this:

assertThat(someList).extracting("someField")

and I want to continue the assert with asserting that only duplicates exist in the extraction. Is there a good way to do this?

1 Answers

If you know what value is expected, containsOnly could be an option:

Verifies that the actual group contains only the given values and nothing else, in any order and ignoring duplicates (i.e. once a value is found, its duplicates are also considered found).

Extending your example:

List<SomeObject> someList = List.of(
  new SomeObject("someValue"),
  new SomeObject("someValue")
);

assertThat(someList)
  .extracting("someField")
  .containsOnly("someValue");
Related