How to Verify JSON Response Content

Hi there,

I’ve spent the last two weeks getting myself familiar with Gatling and I have to say it seems like a very powerful tool. The documentation is very clear and it really helped me in getting to grips with using it. However, I’ve hit a bump in my understanding and I haven’t been able to find the answer anywhere on the web. I’m finding verification very hard. For example, I’ve found many sources that mention that you are able to assert that the content of a JSON response is as expected, but I can’t find a clear example and trial and error is getting me nowhere.

Basically, my JSON response looks like this:

{

“Before”: {

“Overall”: {

“A”: 3031080.3216904188,

“B”: 9527056.329065748,

“C”: 11439447.561616555

}

},

“After”: {

“Overall”: {

“A”: 3030423.469630083,

“B”: 9509912.047610778,

“C”: 1142233.583366325

}

},

“Change”: {

“Overall”: {

“A”: -656.8520603356883,

“B”: -17111.727953303605,

“C”: -17144.281454969198

}

}

}

I have tried the following for my scenario:

val expectedJSON = Jackson.parse(getClass.getResourceAsStream("/whatIfOutputs.json"), UTF_8)

val scnHSVaR =

exec(http(“WhatIf”)

.post("/whatif")

.header(“Content-Type”, “application/json”)

.body(StringBody("${json}"))

.check(bodyString.transform(Jackson.parse).is(expectedJSON))

).pause(1)

which works for the most part, but due to the accuracy of 16 significant figures it sometimes fails due to rounding at this level. In other testing, I have been able to introduce an acceptable epsilon to use for checking. I can’t seem to find a way to compare these to my expected values even as doubles, without even considering the acceptable precision. I tried extracting them using jsonPath, but even then I’m unsure how to convert the result to a double for comparison or how to read and store the expected values from a file to a list.

Where am I going wrong? Is anyone able to provide a basic example of this nature, or point me in the right direction?

Sorry for the long and potentially simple question, I’m just finding that even after reading everything I can find on the subject that there’s a significant knowledge gap here for me. Any help on this is greatly appreciated.

First, this looks to me like a functional test, not a load test. In a load test, you’re not supposed to validate every possible piece of information.
You won’t be able to compare JSON ASTs like you’re trying to do, as they don’t support deep equality.
You’ll have to compare fields one by one, probably with JsonPath + hack your own Validator.

Thanks for your quick response, Stéphane.

Ah, I was hoping to be able to combine load and functionality testing such that verification could take place as well as stress loading in order to use this tool with Jenkins as a check against regressions. A two birds with one stone solution, if you will. Thank you for the suggestion and link, I’ll take a look and see if I can work something in.

Kind regards,

Jacquiline

Well, you can actually use Gatling for functional testing too. Just beware that in functional testing, you’ll be adding much more validations, and those of course eat CPU, so you might want to either have different simulations for load testing, or disable those extra validations then.

Then, what’s tricky in your case is that you want this accuracy based validation, and that’s something you’ll have to hack.

Hey Guys,

Is it possible to compare few <key,value> pairs with Response JSON output dynamically ? My scenario is I can provide some object as like this {key:value, key1:value ,…} and user can add more entries if required and it should dynamically check with response JSON output ?

I described question here. http://stackoverflow.com/questions/40296859/jsonpath-check-hashmap-values-are-present-using-gatling

Thanks,
Swapnil.