0

My JSON response looks like below

{
    "pCategories": [
        "pogc1",
        "pogc16",
        "pogc2",
        "testc1122",
        "testcat10012018",
        "testcat10012019",
        "testcat100120191",
        "testcat11012018",
        "testcat12012018",
        "testcat120120181",
        "testcat20112017",
        "testcat20112018"
    ]
}

I have used the below code to assert.

def slurped = new JsonSlurper().parseText(response.asString())
assert slurped.pCategories.contains("$category")

But getting an error.

How do I resolve it?

0

2 Answers 2

1

Because "$category" is not a String. It is an instance of GStringImpl.

def category = 'pogc16'
assert 'pogc16'.equals("$category") // false

To fix your code you can convert "$category" to String:

assert slurped.pCategories.contains("$category".toString())
Sign up to request clarification or add additional context in comments.

4 Comments

Still i am getting below error assert slurped.pCategories.contains("$category".toString()) | | | | | | | false | testcat21Feb20181144 | | testcat21Feb20181144 | [pogc1, pogc16, pogc2, testc1122, testcat10012018, testcat10012019, testcat100120191, testcat11012018, testcat12012018, testcat120120181, testcat20112017, testcat20112018, testcat21feb20181142, testcat21feb20181144]
at org.codehaus.groovy.runtime.InvokerHelper.assertFailed(InvokerHelper.java:399) at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.assertFailed(ScriptBytecodeAdapter.java:648) at a_orepim_getproduct_categories.getlistofcategories(a_orepim_getproduct_categories.groovy:129)
Oh.. take a look at your values. 'testcat21Feb20181144' != 'testcat21feb20181144' 'F' letter
Yes i tried with converting the category into .toLowerCase(),then it's working.Thanks
0

Need a little help over here. It's still unclear what you're actually trying to do.

If category is a variable, then you don't have to use "$category" in contains(), you can simply use category unless you're evaluating some expression.

But if this is not your use-case and you just want to get rid of the error; simply add an escape character before the $ symbol:

assert slurped.pCategories.contains("\$category")

If you could elaborate on your use-case, maybe we can help.

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.