1

I just wonder if I can validate a value of certain filed in JSON response using Gatling?

currently the code only check if a field presents in the JSON response as following:

val searchTransaction: ChainBuilder = exec(http("search Transactions")
    .post(APP_VERSION + "/transactionDetals")
    .headers(BASIC_HEADERS)
    .headers(SESSION_HEADERS)
    .body(ElFileBody(ENV + "/bodies/transactions/searchTransactionByAmount.json"))
    .check(status.is(200))
    .check(jsonPath("$.example.current.transaction.results[0].amount.value")

If I want to verify the transaction value equals to 0.01, is it possible to achieve this? I googled but didn't find any result, if there is a similar asked before, please let me know I will close this. Thanks.

I tried some assertion in the test, I find the assertion won't fail the performance at all.

val searchTransaction: ChainBuilder = exec(http("search Transactions")
    .post(APP_VERSION + "/transactionDetals")
    .headers(BASIC_HEADERS)
    .headers(SESSION_HEADERS)
    .body(ElFileBody(ENV + "/bodies/transactions/searchTransactionByAmount.json"))
    .check(status.is(200))
    .check(jsonPath("$.example.current.transaction.results[0].amount.value").saveAs("actualAmount"))).
    exec(session => {
    val actualTransactionAmount = session("actualAmount").as[Float]
    println(s"actualTransactionAmount: ${actualTransactionAmount}")
    assert(actualTransactionAmount.equals(10)) // this should fail, as amount is 0.01, but performance test still pass.
    session
  })
Huibin Zhang
  • 1,072
  • 1
  • 15
  • 30

2 Answers2

3

You right, this is a normal solution

.check(jsonPath("....").is("...")))

Such checks are the norm. Since the service may respond and not return for example 5xx status, but there will be an error in the response. So it’s better to check this.

Example: I have application which return status of create user and I checke it

.check(jsonPath("$.status").is("Client created"))
Amerousful
  • 2,292
  • 1
  • 12
  • 26
0

I figure out the way to verify, not sure if that is the best way but it works for me.

val searchTransaction: ChainBuilder = exec(http("search Transactions")
    .post(APP_VERSION + "/transactionDetals")
    .headers(BASIC_HEADERS)
    .headers(SESSION_HEADERS)
    .body(ElFileBody(ENV + "/bodies/transactions/searchTransactionByAmount.json"))
    .check(status.is(200))
    .check(jsonPath("$.example.current.transaction.results[0].amount.value").is("0.01")))

if I change to value 0.02, then test will fail and in the session log it will tell something like below:

---- Errors --------------------------------------------------------------------
> jsonPath($.example.current.transaction.results[0].amount.value).     19 (100.0%)
find.is(0.02), but actually found 0.01
================================================================================

I aware that verify a value in JSON will make it like a functional test, not a load test. In a load test, maybe we are not supposed to validate every possible piece of information. But there is function there if anyone wants to verify something in JSON maybe can reference. Just curious, I still don't know why using assert in previous code won't fail the test though?

Huibin Zhang
  • 1,072
  • 1
  • 15
  • 30