How to achieve the below:
List<Data> streams = new ArrayList<>();
assertThat(streams).usingFieldByFieldElementComparatorIgnoringGivenFields("createdOn").containsOnly(data1, data2);
How to achieve the below:
List<Data> streams = new ArrayList<>();
assertThat(streams).usingFieldByFieldElementComparatorIgnoringGivenFields("createdOn").containsOnly(data1, data2);
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);
Assert.assertTrue(date.after(lowerLimitDate) && date.before(upperLimitDate));
–
Thermoelectrometer usingElementComparatorOnFields("foo", "bar")
was quite useful. –
Cathepsin usingElementComparatorIgnoringFields
is deprecated, now you should use usingRecursiveFieldByFieldElementComparatorIgnoringFields
instead. –
Schaub If you (1) have control of the source code of data
and (2) are using Lombok annotations then you can use the @EqualsAndHashCode.Exclude
against the createdOn
field e.g.
import lombok.*;
@Data
public class Data {
@EqualsAndHashCode.Exclude // will not include this field in the generated equals and hashCode methods.
private Date createdOn;
...
}
This really simplifies the testing:
assertThat(streams).containsOnly(data1, data2);
NOTE: if you do use
equals()
methods on theData
object as part of thesrc/main/java
classes, you will need to ensure the values of the other fields are enough to make the object unique so thatequals()
works properly when using@EqualsAndHashCode.Exclude
.
© 2022 - 2024 — McMap. All rights reserved.