Search code examples
jsoncucumbercucumber-jvm

How to represent nested json objects in a cucumber feature file


I have a need to represent JSON object in the feature file. I could use a json file for this for the code to pick up. But this would mean that i cant pass values from the feature file.

Scenario: Test

Given a condition is met

Then the following json response is sent
 | json |
 | {"dddd":"dddd","ggggg":"ggggg"}|

Above works for a normal json. However if there are nested objects etc then writing the json in a single line like above would make the feature very difficult to read and difficult to fix.

Please let me know.


Solution

  • You can use a string to do that, it makes the json much more legible.

    Then the following json response is sent
      """
       {
          'dddd': 'dddd',
          'ggggg': 'ggggg',
          'somethingelse': {
            'thing': 'thingvalue',
            'thing2': 'thing2value'
          }
        }
      """
    

    In the code, you can use it directly:

    Then(/^the following json response is sent$/) do |message|
      expect(rest_stub.body).to eq(message)
    end
    

    or something like that.