Search code examples
javacucumberautomated-testsbddcucumber-jvm

Using Java objects for Cucumber testing


How can I pass java objects?

Scenario: Java Object test
    When I POST the URL to "/v1/gitlab/project/demo" with <java_object>
    Then I expect to see the response code "200"
    And I expect to see "json" content

How can I pass a java object to cucumber in this manner? Or if not java object, then can i pass a json file?


Solution

  • You can`t pass java objects. You can make a custom transformer through the @Transform annotation but not sure that will help you. I believe for you the best option is to pass JSON string and then create a JSON object from it in the method. For this you will need the following step definition:

    @Given("^I have JSON string \"(\\{(^\"]*\\})\"")
    public void someMethod(String jsonString) {
    }
    

    And then in the feature file you can use the following line:

    Given I have JSON string "{ 'key1' : 'value1', 'key2': 'value2' }"
    

    To keep your test clean you may use examples and refer the exact JSON string as variable. Hope this helps.