Search code examples
diffjavers

Javers:Ignore specific fields in Value Object while comparing two jsons


I am trying to compare two jsons, expected and the API Response using Javers, as part of testing. I want the comparison to exclude the ID parameters that are dynamically generated by response.

My VO is like

public class expectedResponse{

@DiffIgnore
private String id;
private String name;

}

Both my expectedResponse- which is read from excel file and the actual response from API are deserialized into this format and then both the responses are compared.

JsonNode expectedOutput = mapper.readTree(expected.toString());
JsonNode apiResponse = mapper.readTree(actual.toString());
diff=javers.compare(expectedOutput, apiResponse);

But this comparison doesn't exclude/ignore the ID field. Any Idea how I can get it to work? I want only the ID field excluded in comparison results, diff in name should be listed.

Also question 2> I am trying to list the changes from diff

if (diff.hasChanges()) 
    {               

    List<ValueChange> changes=diff.getChangesByType(ValueChange.class);
    for (ValueChange change : changes) 
    {
    logger.info(change.getPropertyName()+ "||" +change.getLeft().toString() + "||" +change.getRight().toString());

change.getPropertyName()- doesnt print the property's name but simply prints "_value" as its value.

Can you pls help in identifying what is going wrong with the code and how can I get this fixed? I am not finding much useful documentations about Javers anywhere in google. Any help is appreciated.


Solution

  • You should compare you domain object instead of object with JsonNode class, look that @DiffIgnore annotation is present only in your domain class and there is no connection between JsonNode and ExpectedResponse, thats why Javers doesn't know to ignore this field.

    To summarise, your code should looks like this:

    ExpectedResponse expectedOutput = ... 
    ExpectedResponse apiResponse = ...
    diff=javers.compare(expectedOutput, apiResponse);