Search code examples
cucumbercucumber-java

Cucumber splits JSON string wrong in Java


When I using Cucumber in Java test, it seems something wrong with the JSON string as input, e.g.

Scenario Outline: not work
Given anythin
When I use <body> as body to call <url>
Then I'll get a status code of <status>

Examples:
| body    | url             | status        | 
| {"id":5}| /rest/update/0  | 404           | 

And the error shows:

You can implement missing steps with the snippets below:

@When("^I use {\"([^\"]*)\":(\\d+)} as body to call \"([^\"]*)\"$")
public void i_use_as_body_to_call(String arg1, int arg2, String arg3) throws Throwable {
// Write code here that turns the phrase above into concrete actions
throw new PendingException();

}

But in fact, the whole JSON string should not be split.


Solution

  • I don't put json directly into a step definition because I think it's too messy. Try these examples.

      Given I have a web service
      And I have "PUT" service for "/test"
      And I am a rest client
      When I "PUT" to the web service with the following
        """
        {"Question": "What is the meaning of life?"}
        """
      Then I receive the expected message
    

    I didn't use any scenario outlines, but I think you could do it. Anyways, that can end up being very messy to read. Also, I usually put all scenario outlines in quotes to keep them clean. One more thing, I split up my json messages inside the step definitions, not directly in the step regexp.