Ignoring only embedded member in AssertJ

Tags: , , ,

I have two classes to test with JUnit/Mockito:

 public class ClassA {

    private int sk;
    // getters and setters


 public class ClassB {

    private int sk;
    private List<ClassA> lista;

    // getters and setters


Then in my test class, using AssertJ:

   List<ClassA> lista = //... populated with list of objects of ClassA

   ClassB expected = new ClassB();

   ClassB actual = getItFromTheClassToTest();   

                    .ignoringFields("sk")  // need to ignore only classA.sk

The problem is that the name sk is in both classes, and I need to ignore it only in ClassA, not in ClassB. Is this possible in AssertJ?


According to the ignoringFields javadoc:

Nested fields can be specified like this: home.address.street.

…so with your classes:

ClassB b = new ClassB(1, Arrays.asList(new ClassA(1)));
ClassB bIsEqualsExceptForNestedField = new ClassB(1, Arrays.asList(new ClassA(2)));
ClassB bIsNotEquals = new ClassB(2, Arrays.asList(new ClassA(3)));

// should succeed
// should fail

Source: stackoverflow