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
Comments
What outcome would like to see ? |
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.
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;
}
}
} |
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! |
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
Note that you don't have to convert collections to arrays for this to work. |
I'm closing this issue as it is going to be addressed in #1002. |
Summary
Provide better reporting on differences (Path to difference:) along with object's toString() during assertion failure when comparing lists of objects
Example
The text was updated successfully, but these errors were encountered: