Visitar URL original
Chaining-friendly assertion that all elements are equal · Issue #4302 · assertj/assertj · GitHub
Skip to content

Chaining-friendly assertion that all elements are equal #4302

Open

Description

@voddan

Feature summary

Provide a chaining-friendly way to assert that all elements are the same:

assertThat(list).allElementsTheSame();

The current best-practice requires using allMatch() and referencing the first element of the list:
assertThat(list).allMatch(e -> e.equals(list.get(0));. However that patter falls short in more complex use cases where the list is produced by filteredOn() and extracting() sequences and cannot be easily referenced.

Example

My current real-world test that verifies that the output table is aligned:

List<String> dataLines = Arrays.stream(output.split("\n"))
	.filter(s -> s.contains("|"))
	.toList();

assertThat(dataLines)
		.hasSize(20)
		.allMatch(s -> s.matches(".* \\| .* --- \\w+"));

List<int[]> pipePositions = dataLines.stream()
	.map(s -> new int[] {s.indexOf('|'), s.indexOf("---")})
	.toList();

assertThat(pipePositions)
		.allMatch(p -> Arrays.equals(p, pipePositions.get(0)));

Note how I cannot join those 2 checks into one because i have to reference the first element.

How it could look like:

assertThat(output.split("\n"))
        .filteredOn(s -> s.contains("|"))
        .hasSize(20)
        .allMatch(s -> s.matches(".* \\| .* --- \\w+"))
        .extracting(s -> new int[] {s.indexOf('|'), s.indexOf("---")})
        .allElementsTheSame()

Special Consideration

As shown in my example, sometimes equals() check on elements is not adequate and a special check is needed (for example Arrays.equals()).

Activity

  1. joel-costigliola commented on Jun 27, 2026

    @joel-costigliola
    Member

    We can add that as I don't see another way of doing such assertions.

    As a side note, instead of

    assertThat(list).allMatch(e -> e.equals(list.get(0));

    you can use

    // contains only ignores duplicates (i.e. once a value is found, its duplicates are also considered found). 
    assertThat(list).containsOnly(list.get(0);

    @voddan would you been keen to contribute this ?

  2. voddan commented on Jun 27, 2026

    @voddan
    Author

    @joel-costigliola yes, I would love to contribute this!

    I can suggest several types of API:

    assertThat(list)
        .allElementsTheSame()
        .allEqual()
        .allFoldPredicate(a, b -> a.equals(b))
        .allExtractToEqualList(s -> List.of(s.index("a"), s.indexOf("b"))
        .allPairsSatisfy(a, b -> assertThat(a).usingRecursiveComparison().isEqualTo(b));

    Which method signatures and method names do you think are best?

  3. joel-costigliola commented on Jun 27, 2026

    @joel-costigliola
    Member

    Let's start with allElementsAreEqual (not the same, which would mean checking by references) and discuss the pair assertion in another issue.

  4. scordio commented on Sep 12, 2026

    @scordio
    Member

    Let's start with allElementsAreEqual

    To stay closer to the existing containsOnlyNulls, what about containsOnlySameElement containsOnlyEqualElements?

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions