How to Verify JSON Response Content

2,637 views
Skip to first unread message

Jacquiline

unread,
Oct 21, 2014, 12:31:47 PM10/21/14
to gat...@googlegroups.com

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.

Stéphane Landelle

unread,
Oct 22, 2014, 4:49:34 PM10/22/14
to gat...@googlegroups.com
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.

--
You received this message because you are subscribed to the Google Groups "Gatling User Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to gatling+u...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Jacquiline

unread,
Oct 24, 2014, 5:46:53 AM10/24/14
to gat...@googlegroups.com
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

Stéphane Landelle

unread,
Oct 24, 2014, 5:52:09 AM10/24/14
to gat...@googlegroups.com
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.



--

Swapnil Kotwal

unread,
Oct 28, 2016, 9:46:13 PM10/28/16
to Gatling User Group, slan...@excilys.com
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 ?


Thanks,
Swapnil.
Reply all
Reply to author
Forward
0 new messages