Reputation: 811
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?
Upvotes: 0
Views: 5812
Reputation: 34014
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.
Upvotes: 2
Reputation: 481
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.
Upvotes: 1