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

Better reporting on usingRecursiveFieldByFieldElementComparator() for list of objects #932

Closed
harishkannarao opened this issue Feb 22, 2017 · 5 comments
Labels
theme: recursive comparison An issue related to the recursive comparison

Comments

@harishkannarao
Copy link

harishkannarao commented Feb 22, 2017

Summary

Provide better reporting on differences (Path to difference:) along with object's toString() during assertion failure when comparing lists of objects

Example

@Test
    public void sampleTest() throws Exception {
        List<MyClass> myClasses = Arrays.asList(
                new MyClass("type1", new MySubClass("sub_type1")),
                new MyClass("type2", new MySubClass("sub_type2"))
        );

        assertThat(myClasses)
                .usingRecursiveFieldByFieldElementComparator()
                .containsExactly(
                        new MyClass("type1", new MySubClass("sub_type1")),
                        new MyClass("type2", new MySubClass("sub_type3"))
                );

    }

    public static class MyClass {
        private final String type;
        private final MySubClass mySubClass;

        public MyClass(String type, MySubClass mySubClass) {
            this.type = type;
            this.mySubClass = mySubClass;
        }
    }

    public static class MySubClass {
        private final String type;

        public MySubClass(String type) {
            this.type = type;
        }
    }
@joel-costigliola
Copy link
Member

What outcome would like to see ?

@harishkannarao
Copy link
Author

harishkannarao commented Feb 22, 2017

I have enhanced the sample test. On failure, the second test gives additional information Path to difference: as which helps to quickly identify the elements that differ between lists.

Path to difference:  <mySubClass.type>
- expected: <sub_type3>
- actual  : <sub_type2>
package com.harishkannarao;

import org.junit.Test;

import java.util.Arrays;
import java.util.List;

import static org.assertj.core.api.Assertions.assertThat;

public class SampleListComparisonTest {

    @Test
    public void listComparisonConventional() throws Exception {
        List<MyClass> myClasses = Arrays.asList(
                new MyClass("type1", new MySubClass("sub_type1")),
                new MyClass("type2", new MySubClass("sub_type2"))
        );

        assertThat(myClasses)
                .usingRecursiveFieldByFieldElementComparator()
                .containsExactly(
                        new MyClass("type1", new MySubClass("sub_type1")),
                        new MyClass("type2", new MySubClass("sub_type3"))
                );

    }

    @Test
    public void listComparisonHackyWay() throws Exception {
        List<MyClass> myClassesActual = Arrays.asList(
                new MyClass("type1", new MySubClass("sub_type1")),
                new MyClass("type2", new MySubClass("sub_type2"))
        );

        List<MyClass> myClassesExpected = Arrays.asList(
                new MyClass("type1", new MySubClass("sub_type1")),
                new MyClass("type2", new MySubClass("sub_type3"))
        );


        assertThat((Object) myClassesActual.toArray())
                .isEqualToComparingFieldByFieldRecursively(
                        myClassesExpected.toArray()
                );

    }

    public static class MyClass {
        private final String type;
        private final MySubClass mySubClass;

        public MyClass(String type, MySubClass mySubClass) {
            this.type = type;
            this.mySubClass = mySubClass;
        }
    }

    public static class MySubClass {
        private final String type;

        public MySubClass(String type) {
            this.type = type;
        }
    }
}

@joel-costigliola joel-costigliola added this to the 2.8.0 milestone Mar 26, 2017
@jomora
Copy link

jomora commented May 19, 2017

I have to compare very deep structures of objects with many attributes. This is quite tedious and I'm really looking forward to be able to use this feature! :-) Thx in advance!

@joel-costigliola joel-costigliola modified the milestones: 2.8.0, 2.9.0 May 20, 2017
@joel-costigliola
Copy link
Member

I agree with the issue raised, it turns out it is not as simple to fix with the current design, I'm gonna add this as another requirement for #1002 (New recursive comparison api).

In the meantime the best way to have detailed error is to cast the collection to Object and use isEqualToComparingFieldByFieldRecursively as suggested by @harishkannarao:

assertThat((Object) myClassesActual)
                .isEqualToComparingFieldByFieldRecursively(myClassesExpected);

Note that you don't have to convert collections to arrays for this to work.

@joel-costigliola joel-costigliola added theme: recursive comparison An issue related to the recursive comparison and removed improvement labels Oct 29, 2017
@joel-costigliola joel-costigliola removed this from the 2.9.0 milestone Oct 29, 2017
@joel-costigliola
Copy link
Member

I'm closing this issue as it is going to be addressed in #1002.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
theme: recursive comparison An issue related to the recursive comparison
Projects
None yet
Development

No branches or pull requests

4 participants