Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Different enum classes with same names result in the same output for expected and actual when comparing lists #3083

Open
martinfrancois opened this issue Jun 15, 2023 · 1 comment
Labels
type: improvement A general improvement

Comments

@martinfrancois
Copy link
Contributor

Feature summary

When you have two enum classes with the same name but in different packages and compare an enum object in a list from one package with an enum object of the other package, it fails, but the output is confusing, as it suggests that both are the same. The fully qualified name should be added to make it easier to differentiate the two.

Example

org.example.pkg1.MyEnum.java:

package org.example.pkg1;

public enum MyEnum {
    CONSTANT1, CONSTANT2
}

org.example.pkg2.MyEnum.java:

package org.example.pkg2;

public enum MyEnum {
    CONSTANT1, CONSTANT2
}

Test:

    @Test
    public void reproduce() {
        List<Object> personList = List.of(org.example.pkg1.MyEnum.CONSTANT1);

        assertThat(personList)
                .contains(org.example.pkg2.MyEnum.CONSTANT1);
    }

Results in the following error:

java.lang.AssertionError: 
Expecting List12:
  [CONSTANT1]
to contain:
  [CONSTANT1]
but could not find the following element(s):
  [CONSTANT1]

Nicer would be something like that:

java.lang.AssertionError: 
Expecting List12:
  [CONSTANT1 (org.example.pkg1.MyEnum)]
to contain:
  [CONSTANT1 (org.example.pkg2.MyEnum)]
but could not find the following element(s):
  [CONSTANT1 (org.example.pkg1.MyEnum)]
@scordio scordio added the type: improvement A general improvement label Jun 15, 2023
@scordio
Copy link
Member

scordio commented Jun 15, 2023

This could seem to be connected to #3084 but it's not, because the underlying error message factory ShouldContain doesn't rely on UnambiguousRepresentation. Also, there are other assertions/error messages that are affected (e.g., containsExactly).

We should evaluate how to efficiently refactor this area to address everything in a uniform way.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: improvement A general improvement
Projects
None yet
Development

No branches or pull requests

2 participants