Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to exclude some fields from assertJ usingFieldByFieldElementComparator?

How to achieve the below:

List<Data> streams = new ArrayList<>();
assertThat(streams).usingFieldByFieldElementComparatorIgnoringGivenFields("createdOn").containsOnly(data1, data2);
like image 528
Vel Ganesh Avatar asked Mar 23 '18 10:03

Vel Ganesh


People also ask

What is recursive comparison?

Strict/lenient recursive comparison Compatible means that the expected object/field types are the same or a subtype of actual/field types, for example if actual is an Animal and expected a Dog , they will be compared fiels by field in strict type checking mode.

What is AssertJ used for?

The AssertJ project provides fluent assertion statements for test code written in Java. These assert statements are typically used with Java JUnit tests. The base method for AssertJ assertions is the assertThat method followed by the assertion.


1 Answers

Use ListAssert.usingElementComparatorIgnoringFields(String... fields) that does the same thing as ListAssert.usingFieldByFieldElementComparator() but by allowing to ignore some fields/properties :

Use field/property by field/property comparison on all fields/properties except the given ones

So you could write :

List<Data> streams = new ArrayList<>();
//...
Assertions.assertThat(streams)
          .usingElementComparatorIgnoringFields("createdOn")
          .containsOnly(data1, data2);
like image 186
davidxxx Avatar answered Oct 15 '22 01:10

davidxxx