5

I am doing a simple rest api test in Cucumber Java. The response is in Json format.

The gherkin feature file i write looks like:

  Scenario:  
    Given I query service by "employees"
    When I make the rest call
    Then response should contain:
      """
      {"employees":[
      {"firstName":"John", "lastName":"Doe"},
      {"firstName":"Anna", "lastName":"Smith"},
      {"firstName":"Peter", "lastName":"Jones"}
      ]}
      """

Now since there are multiple query to test with different parameters like "employees", "departments".. etc, it's natural to write Scenario Outline to perform the task:

  Scenario Outline: 
    Given I query service by "<category>"
    When I make the rest call
    Then response should contain "<json_string_for_that_category>"
    Examples:
      | category     | json_string_for_that_category     |
      | employee     | "json_string_expected_for_employee"  |
      | department   | "json_string_expected_for_department"|

where json_string_expected_for_employee is just:

  {"employees":[
  {"firstName":"John", "lastName":"Doe"},
  {"firstName":"Anna", "lastName":"Smith"},
  {"firstName":"Peter", "lastName":"Jones"}
  ]}

by copy and paste.

But there are problems with this approach:

  1. There are special characters in Json string need to escape if just surrounded by " "
  2. The Scenario Outline table looks very messy

What is a good approach to do this? Can a string variable be defined else where in feature file to store long strings, and this variable name placed in the table?

Or any solutions? This must be a common scenario for people to compare non-trivial data output in Cucumber.

Thanks,

user1559625
  • 2,583
  • 5
  • 37
  • 75
  • There is an error in your step 'Then response should contain ""' which should read 'Then response should contain ""'. Also, you are putting queries for 2 different services in one scenario outline. I wouldn't do that. – Dave McNulla Dec 23 '15 at 22:19

2 Answers2

1

For your problem 1

You have to use escape character backslash (\)

example: \"employees\" instead of "employees"

For your problem 2

It is common that if your input is not in the similar length of a character, it will be messy. You can use indent to make it clear.

or

Use separate java file to store all the input as variable and pass it to scenario outline examples while execution.

Aravin
  • 6,605
  • 5
  • 42
  • 58
1

First of all resist the temptation to use scenario outlines, generally they are not worth the hassle.

Personally I don't think you get any value in having the json string actually in the feature. So I would write something like:

Scenario: Query by category
  Given there is a category
  When I query the service by the category
  Then I should receive a json representation of the category

This allows me to assign the responsibility of defining what a json representation of a category should be to code. This code could be in our step definitions, or it could even be in our application (perhaps shared with unit tests). Some of things this code could do are:

  1. Actually validate that the response is valid json
  2. Validate the structure of the json matches the structure of a category
  3. Validate values e.g. the category id

One thing this approach will do is stop you having to rewrite all your scenarios when you add an extra field to a Category.

diabolist
  • 3,990
  • 1
  • 11
  • 15