I'm working on writing some tests for a function that takes some user supplied data and returns a JSON string based on a template. Up until now, I have simply been doing assertions that compare the entire output string against an 'expected' json document. It works, but the tests are brittle. Any change to the output requires updating the expected document, even if the change is unrelated to what is being tested.
What I should be doing is making assertions against specific sections of the json output, but I am struggling to find a library that does what I want. JsonPath can get the job done, but the syntax is unwieldy. I know where the particular data I want to test is, but trying to craft the exact JsonPath query string to retrieve it is a time consuming slog.
What I'm hoping to find is a library that allows me to add a 'tag' comment to a json element that I can use in my tests to get the exact data I need. Something like:
{
"elementType": "formInputHidden",
"name": "message_id",
"value": "274"
},
{
"elementType": "formInputHidden",
"name": "message_status",
"value": "PENDING_MODERATOR_APPROVAL"
}
Then in my test I could do a lookup like:
Object result = jsonFind.get(jsonOutput, "name", "message_id");
//result="274"
Does anything like this exist?
You may try library Josson & Jossons.
https://github.com/octomix/josson
Josson Query
The corresponding Josson query is simple.
Jossons Statement
Jossons supports multiple datasets manipulation. Therefore, dataset name qualifier is required in the statement.
Dynamic Jossons Statement
Changing the evaluation criteria to placeholders.