Skip to content

contains / doesNotContain assertions do not follow Set::contains semantics with array elements #4175

@scordio

Description

@scordio

While investigating #4157, I noticed that contains / doesNotContain assertions have the opposite behavior of Set::contains in case of instances with array elements.

For example:

Set<String[]> actual = Set.of(array("Homer"), array("Marge"));
String[] expected = array("Homer");

assertThat(actual.contains(expected)).isFalse(); // succeeds

assertThat(actual).contains(expected); // succeeds
assertThat(actual).doesNotContain(expected); // fails

This is due to the current implementation of iterableContains in StandardComparisonStrategy:

return Streams.stream(iterable).anyMatch(object -> areEqual(object, value));

which delegates the comparison to areEqual, mimicking Objects::deepEquals under the hood.

However, when introducing the changes in #4157 to retain collection semantics, such a deep array comparison of collection elements is no longer performed, and the issue manifests in the current test cases.

Pinned by scordio

Metadata

Metadata

Assignees

Labels

No labels
No labels

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions