Skip to content

feat: add allElementsAreEqual assertion for iterables - #4330

Open
seonwooj0810 wants to merge 1 commit into
assertj:mainfrom
seonwooj0810:feat/issue-4302-all-elements-are-equal
Open

seonwooj0810 wants to merge 1 commit into
assertj:mainfrom
seonwooj0810:feat/issue-4302-all-elements-are-equal

Conversation

@seonwooj0810

Copy link
Copy Markdown

Closes #4302

Why

There is currently no chaining-friendly way to assert that all elements of an iterable are equal to each other. The workaround assertThat(list).allMatch(e -> e.equals(list.get(0))) requires a direct reference to the first element, which is awkward when the iterable is produced by a chain of filteredOn/extracting calls and cannot be easily referenced. As discussed in the issue, @joel-costigliola agreed to add allElementsAreEqual (equality-based, not reference-based).

What

Adds allElementsAreEqual() to AbstractIterableAssert, so it is available on all Iterable-based assertions:

assertThat(dataLines).allElementsAreEqual();
  • Passes when all elements are equal, and (vacuously) for empty or single-element iterables — consistent with allMatch/allSatisfy.
  • Fails otherwise, reporting every element that is not equal to the first.
  • Honors a custom element comparator set via usingElementComparator(...) (the assertion goes through the internal comparisonStrategy).

New error factory ShouldHaveAllElementsEqual produces a message such as:

Expecting all elements to be equal to:
  "a"
but the following element(s) were not:
  ["b"]
in:
  ["a", "b"]

Tests

  • Iterables_assertAllElementsAreEqual_Test — engine behaviour: all-equal, single-element and empty pass; null actual; failure reports all unequal elements; readable error message; and the custom (case-insensitive) comparison-strategy pass/fail paths.
  • IterableAssert_allElementsAreEqual_Test — verifies the public API delegates to the internal engine (IterableAssertBaseTest).

Verification done: ran ./mvnw -pl assertj-core test for both new test classes (10 tests, all green); spotless:apply and license:format produce no further changes.

I followed the discussion in #4302; if you would prefer this also exposed on ObjectEnumerableAssert (arrays) or want the pair/fold assertions tracked separately, happy to adjust.

@seonwooj0810

Copy link
Copy Markdown
Author

Circling back on this assertion addition — happy to tweak the naming or API shape if you have preferences.

@scordio

scordio commented Sep 12, 2026

Copy link
Copy Markdown
Member

Hi @seonwooj0810, thanks for your contribution!

I'd like to discuss an alternative name for this feature; see #4302 (comment).

@seonwooj0810

Copy link
Copy Markdown
Author

Thanks @scordio! Happy to rename. One thing to flag: in #4302 @joel-costigliola steered away from "same" since it could read as reference identity, which is why I went with allElementsAreEqual. Keeping the containsOnly* family is a good point though — would something like containsOnlyEqualElements work for both concerns? I'll update the PR once you two settle on the name.

@scordio

scordio commented Sep 23, 2026

Copy link
Copy Markdown
Member

Fair enough, comment updated for consistency. WDYT @joel-costigliola?

@scordio

scordio commented Sep 24, 2026

Copy link
Copy Markdown
Member

@seonwooj0810 just FYI, there is no need to polish your PR description or comments through an LLM. We much prefer direct and authentic conversation, as keeping things direct makes the review process faster and more natural for everyone.

@seonwooj0810

Copy link
Copy Markdown
Author

Understood, thanks for the feedback. I’ll keep the discussion brief and direct.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Chaining-friendly assertion that all elements are equal

2 participants