0

Is there a best practice in comparing two different objects e.g. two Dtos and if they differ to not show a long result where it is difficult to find the property which differs if the object is somehow complex and nested?

Currently I see a long result where it is difficult or cumbersome to find which specific propery failed. At least it is using a data class using Kotlin.

enter image description here

enter image description here

And in the IDE e.g. IntelliJ you have to scroll to see the full comparision if the object is bigger or more complex. It is not intuitive or easy to find where the difference is.

What I want to avoid is e.g. to use assertions for every property and only check the properties against each other.

In my opinion this leads to a maintenance overhead.

For example I can see using Kotest this nice output. Why is this not possible with Junit5 assertEquals method?

org.opentest4j.AssertionFailedError: data class diff for com.lennykey.Cat
├ name: expected:<"Catta"> but was:<"Catty">
├ favouriteDish: expected:<"Tuna"> but was:<"Fish">
└ father: Expected null but actual was Cat(name=Father, color=orange, birthDay=1.01.1980, address=Street 1, favouriteDish=Fish, mother=null, father=null)

expected:<Cat(name=Catta, color=orange, birthDay=1.01.1980, address=Street 1, favouriteDish=Tuna, mother=null, father=null)> but was:<Cat(name=Catty, color=orange, birthDay=1.01.1980, address=Street 1, favouriteDish=Fish, mother=null, father=Cat(name=Father, color=orange, birthDay=1.01.1980, address=Street 1, favouriteDish=Fish, mother=null, father=null))>
Expected :Cat(name=Catta, color=orange, birthDay=1.01.1980, address=Street 1, favouriteDish=Tuna, mother=null, father=null)
Actual   :Cat(name=Catty, color=orange, birthDay=1.01.1980, address=Street 1, favouriteDish=Fish, mother=null, father=Cat(name=Father, color=orange, birthDay=1.01.1980, address=Street 1, favouriteDish=Fish, mother=null, father=null))
lennykey
  • 1,195
  • 2
  • 12
  • 25
  • This is something either the `assert...` methods or the test runners (e.g., in your IDE) do. – Robert Apr 26 '21 at 16:07

1 Answers1

0

Not sure that I got your problem correctly, but I'll try to answer:

Assumptions:

  1. You are using IDE and it is Idea
  2. You are using Java + jUnit5
  3. You are comparing two objects of the same type, so you have equals()/hashCode() overridden.
  4. toString() is overridden to provide some meaningful output.

Lets compare two objects of class Cat:

public final class Cat {
  private String name;
  private int age;
}

Test:

  @Test
  public void compareCats() {
    Cat cat1 = new Cat("Simple cat", 15);
    Cat cat2 = new Cat("Super cat", 15);

    Assertions.assertEquals(cat1, cat2);
  }

Let's run the test and see results:

enter image description here

Let's click on generated "" link in console output:

enter image description here

So inside diff window you can configure how you want to highlight differences. Also you can easily navigate from one difference to another.

  • Hi @Andrey thank you very much for answering and yes you got it completely right. You pictures visualze what I wanted to express. – lennykey May 19 '21 at 19:56
  • I also added a picture of the result where it starts to get difficult to see where exactly the difference exactly is – lennykey May 19 '21 at 20:02