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(); expected.setSk(5); expected.setLista(lista); ClassB actual = getItFromTheClassToTest(); assertThat(actual).usingRecursiveComparison() .ignoringFields("sk") // need to ignore only classA.sk .isEqualTo(expected);
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
?
Advertisement
Answer
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 assertThat(b) .usingRecursiveComparison() .ignoringFields("lista.sk") .isEqualTo(bIsEqualsExceptForNestedField); // should fail assertThat(b) .usingRecursiveComparison() .ignoringFields("lista.sk") .isEqualTo(bIsNotEquals);