jmes path assertion failing in jmeter

为君一笑 提交于 2020-12-27 05:52:31

问题


In my jmeter test, the test receives the following json response.

{"result":"success","additional-info":"{\"external-profile\":{\"email\":\"myemail@gmail.com\",\"firstname\":\"fn\",\"lastname\":\"ln\",\"portfolio\":{\"tags-of-interest\":[],\"question-created-tags\":[{\"tag\":\"un2-new tag-empty\",\"count\":1},{\"tag\":\"un2-new tag2-empty\",\"count\":1}],\"question-answered-tags\":[]}}}"}

I want to check that the message has path additional-info.exernal-profile.portfolio and that there are keys tags-of-interest, question-created-tag,question-answered-tag

I am using json jmes path but I am getting error Assertion failure message:Invalid argument type calling "keys": expected object but was null

What am I doing wrong?


回答1:


Just go for 2 JSON JMESPath Extractors:

  1. Extract the content of additional-info attribute into a JMeter Variable from the response

  2. Extract the attributes from the external-profile attribute:

You can see the extracted values using Debug Sampler and View Results Tree listener combination:


Be aware that since JMeter 3.1 you should be using JSR223 Test Elements and Groovy language for scripting as:

  • Groovy has built-in JSON support
  • Groovy is more Java-compliant when it comes to modern language features support (lambdas, generics, etc.)
  • Groovy has better performance
  • Groovy provides improvements on top of Java SDK

More information: Apache Groovy - Why and How You Should Use It




回答2:


The problem was that additional-info was a string and not an object. I used minimal-json-0.9.5 and beanshell post processor scripting to convert the strings into json objects.

Beanshell code

import com.eclipsesource.json.*;

//prev.setSuccessful(false);
try {
    String jsonString = prev.getResponseDataAsString(); //response as string
    log.info("received json string: "+jsonString);
    
    JsonObject responseAsJsonObject = JsonObject.readFrom(jsonString); //convert response string as json
    log.info("converted to object: "+responseAsJsonObject);
    
    String additionalInfoString = responseAsJsonObject.get("additional-info").asString(); //get additional info string from json object
    log.info("additional info as string: "+additionalInfoString);
    
    JsonObject additionalInfoJsonObject = JsonObject.readFrom(additionalInfoString); //convert additional info string to json
    log.info("additional info as object: "+additionalInfoJsonObject);
    
    JsonObject externalProfileJsonObject = additionalInfoJsonObject.get("external-profile").asObject(); //get external profile object
    log.info("external profile as object: "+externalProfileJsonObject);
    
    JsonObject portfolioJsonObject = externalProfileJsonObject.get("portfolio").asObject(); //get portfolio object
    log.info("portfolio as object: "+portfolioJsonObject);
    
    JsonArray tagsOfInterest = portfolioJsonObject.get("tags-of-interest").asArray();
    log.info("tags of interest: "+tagsOfInterest);
    
    JsonArray tagsCreated = portfolioJsonObject.get("question-created-tags").asArray();
    log.info("tags created: "+tagsCreated);
    
    JsonArray tagsAnswered = portfolioJsonObject.get("question-answered-tags").asArray();
    log.info("tags tagsAnswered: "+tagsAnswered);
//  prev.setSuccessful(true);
} catch (Exception e){
    log.info("error in processing beanshell script: ", e);
    prev.setSuccessful(false);
}



来源:https://stackoverflow.com/questions/65151285/jmes-path-assertion-failing-in-jmeter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!