italktothewind
italktothewind

Reputation: 2195

Karate test framework: contains only with multiline JSON

I'm using Karate test framework.

This feature is working:

Given path '/endpoint'
When method GET
Then match response.list contains only { "field1": "value1", "field2": "value2"}

But I don't want to write the whole JSON in a single line, so I tried:

Given path '/endpoint'
When method GET
Then match response.list contains only
"""
{
  "field1": "value1",
  "field2": "value2"
}
"""

I'm getting this error:

 Tests in error: 
  example.feature:11 - more than one step-definition method matched: match response.list contains only - [public void com.intuit.karate.StepActions.matchDocstring(java.lang.String,java.lang.String,java.lang.String) [response.list contains , only], public void com.intuit.karate.StepActions.match(java.lang.String,java.lang.String,java.lang.String) [response.list , contains,  only]]

Is there any way to use contains only and also use multiline JSONs? Thanks in advance

Upvotes: 1

Views: 4699

Answers (1)

Peter Thomas
Peter Thomas

Reputation: 58058

Yes this is a parser edge case, just split into 2 steps:

Given def response = { list: { "field1": "value1", "field2": "value2" } }
And def expected =
"""
{ "field1": "value1", "field2": "value2"}
"""
Then match response.list contains only expected

Please note that contains only makes sense only for JSON arrays, think about it please.

Upvotes: 4

Related Questions