1

Doing REST API Testing using Jmeter. when I am Using JSON Path Tester in HTTP Response and provided JSON Path Expression as $..name. Getting following matches

Done,
Won't Do,
Duplicate,
Cannot Reproduce

enter image description here

But when I place the same JSON expression and Result in JSON Assertion, getting the error:-

Assertion error: false
Assertion failure: true
Assertion failure message: Value expected to match regexp '"Done","Won't Do","Duplicate","Cannot Reproduce"', but it did not match: '["Done","Won't Do","Duplicate","Cannot Reproduce"]'

enter image description here

0

2 Answers 2

0

To match multiple values

  1. Extract all the matching values from JSON Response using JSON Extractor
  2. Use JSR223 Assertion with following code:

    int nameLength = Integer.parseInt(vars.get("Name_matchNr"));
    
    String[] responseArray = new String[nameLength];
    for (int i = 0; i < nameLength; i++) {
        responseArray[i] = vars.get("Name_"+(i+1));
    }
    
    boolean test1 = Arrays.asList(responseArray).contains("Done");
    boolean test2 = Arrays.asList(responseArray).contains("Won't Do");
    boolean test3 = Arrays.asList(responseArray).contains("Duplicate");
    boolean test4 = Arrays.asList(responseArray).contains("Cannot Reproduce");
    
    if (test1 == false) {
    AssertionResult.setFailureMessage("Done not present in Response");
    AssertionResult.setFailure(true);
    }
    if (test2 == false) {
    AssertionResult.setFailureMessage("Won't Do not present in Response");
    AssertionResult.setFailure(true);
    }
    if (test3 == false) {
    AssertionResult.setFailureMessage("Duplicate not present in Response");
    AssertionResult.setFailure(true);
    }
    if (test4 == false) {
    AssertionResult.setFailureMessage("Cannot Reproduce not present in Response");
    AssertionResult.setFailure(true);
    }
    

enter image description here

To match single value

  1. Use JSON Extractor to extract the values
  2. Use JSON Assertion to Assert the values. In Expected Value section enter any single value : "Done/Won't Do/Duplicate/Cannot Reproduce"

enter image description here

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks. Yes That I am able to do - one single match. But trying to verify more than one.
0

If this is really what you're trying to achieve, consider the following JSON Assertion configuration:

enter image description here

Just in case "Expected Value" textual representation:

["Done","Won't Do","Duplicate","Cannot Reproduce"]

However be informed that this form of assertion will be fragile and sensitive to any JSON changes, i.e. if the order of matches will not be the same - the assertion will fail. So I would rather recommend using one of the approaches described in The Easiest Way To Compare REST API Responses Using JMeter article.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.