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?

Evgeny Smirnov
  • 2,886
  • 1
  • 12
  • 22
Hanumanth
  • 102
  • 1
  • 1
  • 8

2 Answers2

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())
Evgeny Smirnov
  • 2,886
  • 1
  • 12
  • 22
  • 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] – Hanumanth Feb 21 '18 at 07:15
  • 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) – Hanumanth Feb 21 '18 at 10:36
  • Oh.. take a look at your values. 'testcat21Feb20181144' != 'testcat21feb20181144' 'F' letter – Evgeny Smirnov Feb 21 '18 at 12:46
  • Yes i tried with converting the category into .toLowerCase(),then it's working.Thanks – Hanumanth Feb 23 '18 at 06:37
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.

dev-eloper
  • 110
  • 11