Skip to content

assertThat(collection).doesNotHaveDuplicates() is not sorted, even if the collection is #3331

@Bananeweizen

Description

@Bananeweizen

The error message of assertThat(collection).doesNotHaveDuplicates() lists the duplicates in a different order than the elements of the collection actually have. That is not an error per-se, but can make it harder to find the root cause of the failing assertion. So this might also be labeled improvement instead of bug.

  • assertj core version: 3.25.1

Test case reproducing the bug

    @Test
    void sortOrder() throws Exception {
        var list = List.of("org.objectweb.asm.source",
                "org.eclipse.emf.compare.diagram.gmf.source.feature.group",
                "org.eclipse.emf.compare.rcp.ui.source.feature.group",
                "org.objectweb.asm",
                "org.eclipse.jgit.feature.group",
                "org.eclipse.emf.compare.diagram.gmf.source.feature.group",
                "org.objectweb.asm",
                "org.apache.httpcomponents.httpclient",
                "org.eclipse.emf.compare.rcp.ui.source.feature.group",
                "org.eclipse.jgit.feature.group",
                "org.eclipse.emf.compare.ide.ui.source.feature.group");
        assertThat(list.stream().sorted()).doesNotHaveDuplicates();
    }

Note that the list gets sorted in the assertion. Still the output is not sorted, because the duplicates are collected in a set which is not insertion order stable.

Found duplicate(s):
  ["org.eclipse.emf.compare.diagram.gmf.source.feature.group",
    "org.eclipse.emf.compare.rcp.ui.source.feature.group",
    "org.objectweb.asm",
    "org.eclipse.jgit.feature.group"]
in:
  ["org.apache.httpcomponents.httpclient",
    "org.eclipse.emf.compare.diagram.gmf.source.feature.group",
    "org.eclipse.emf.compare.diagram.gmf.source.feature.group",
    "org.eclipse.emf.compare.ide.ui.source.feature.group",
    "org.eclipse.emf.compare.rcp.ui.source.feature.group",
    "org.eclipse.emf.compare.rcp.ui.source.feature.group",
    "org.eclipse.jgit.feature.group",
    "org.eclipse.jgit.feature.group",
    "org.objectweb.asm",
    "org.objectweb.asm",
    "org.objectweb.asm.source"]

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions