Comparison strategies - TestMonkeys/jEntityTest GitHub Wiki

There are different comparison strategies that can be applied on a field, by using a specific annotation. Below is the complete list:

Index

Default Comparison

If a field has no annotations defining the comparison strategy - a default strategy will be applied.

IgnoreComparison

the comparison will completely ignore the field. This is useful on fields that are generated during object creation.

Annotation Yaml Strategy Name
@IgnoreComparison IgnoreComparison

Let's look at an example, a Model entity that has a token property, that is updated each time it is retrieved via API, there is no way to know what this value is, and it makes no difference when comparing an expected entity with the actual:

public class Model{

  @IgnoreComparison
  private String token;
Expected Actual Comparison Result
model.setToken("") model.setToken("anything") Passed

StringComparison

The string comparison strategy allows custom comparison for String fields by allowing you to choose if it is case sensitive, trim before compare or ignore certain characters

Annotation Yaml Strategy Name
@StringComparison StringComparison

You can customize the strategy using the parameters:

Parameter type default note
caseSensitive boolean true if set to false - will ignore the case while comparing the strings
trim boolean false if set to true - will trim both strings before comparison
ignoreCharacters char[] empty will ignore any occurrence of the specified characters before comparison

This comparison strategy is useful for cases when the actual field might be altered, to contain newLine characters, or contains additional spaces before or after.

public class Model{

  @StringComparison
  private String name;
Annotation Expected Actual Comparison Result
@StringComparison model.setName("John") model.setName("john") Failed
@StringComparison(caseSensitive = false) model.setName("John") model.setName("john") Passed
@StringComparison(caseSensitive = false, ignoreCharacters = {'\n', '\r'}) model.setName("John") model.setName("john\r\n") Passed
@StringComparison(trim=true) model.setName("John") model.setName(" John ") Passed

DateTimeComparison

DateTime comparison allows setting an allowed delta between the values. This can be useful when checking timestamps created on server, by setting the delta to a few seconds.

Annotation Yaml Strategy Name
@DateTimeComparison DateTimeComparison

You can customize the strategy using the parameters:

Parameter type default note
delta int 0 maximum allowed difference between the actual and expected time
unit ChronoUnit NANOS time difference measuring unit, for yaml model the enum value should be used, like SECONDS, MINUTES, etc..

The following example shows a model where we accept a bit of a time difference between two entity values:

public class Model {

    @DateTimeComparison(delta = 5, unit = ChronoUnit.SECONDS)
    private LocalDateTime createdAt;
Expected Actual Comparison Result
model.createdAt(LocalDateTime.now()) model.createdAt(LocalDateTime.now().plusSeconds(4)) Passed
model.createdAt(LocalDateTime.now()) model.createdAt(LocalDateTime.now().plusSeconds(6)) Failed

ChildEntityComparison

Child Entity comparison allows for comparing model properties that are themselves models.

Annotation Yaml Strategy Name
@ChildEntityComparison ChildEntityComparison

The following example shows the usage scenario of a child entity comparison, where your model contains a property that itself would have one or more properties.

public class Model {

    @ChildEntityComparison
    private AddressModel address;
Expected Actual Comparison Result
model.setAddress(new AddressModel("street 1")) model.setAddress(new AddressModel("street 1")) Passed
model.setAddress(new AddressModel("street 1")) model.setAddress(new AddressModel("other address 1")) Failed

ChildEntityListComparison

Child Entity List comparison allows for model based comparison of lists

Annotation Yaml Strategy Name
@ChildEntityListComparison ChildEntityListComparison

You can customize the strategy using the parameters:

Parameter type default note
ordered boolean true if set to false - will pass if the same items are present in both expected and actual but in a different order

Usage example:

public class Model {

    @ChildEntityListComparison
    private List<Contact> contacts;
⚠️ **GitHub.com Fallback** ⚠️