0

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?

2 Answers 2

2

A lot easier to read would be to use a datatable for this:

Given I have the following data:
 | property1 | property2 |
 | abc       | 123       |

With the following step definition:

@Given("^I have the following data:$")
public void given_data(DataTable table) {
    final List<Map<String, String>> rows = table.asMaps(String.class, String.class);
    final Map<String, String> data = rows.get(0);
    final String property1 = data.get("property1");
}

I would also suggest only specifying the properties that are relevant for the business case, and add any technical properties inside the step definition. Talking about POST and specific URLs is also already to technical for a cucumber scenario, at least in my opinion.

Sign up to request clarification or add additional context in comments.

1 Comment

I have been following the same approach and saw someone thinking alike. (y)
1

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.

2 Comments

Then that JSON String will have an invalid format right? I can't have a single quote. It is considered invalid i think. I will have to escape all the double quotes? "{ \"key1\" : \"value1\", \"key2\": \"value2\" }".. ?
Oh yes. You will need to escape them if using directly in the line. If using example tables it may work without escaping

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.