Question

This question isn't about REST, but about using the returned value from an invocation made in @When in the subsequent @Then.

I am looking at using JBehave to test some calls to a REST api. First there is a post to create the user

When I create a user with name Charles Darwin

As I understand REST, and this is what the Atom api does, the id is returned in the location header, e.g. /user/22. So then I want to assert something about the response.

Then user was created with a valid Id

I can do this by creating a member variable in the Steps class and storing the response there, and I have used this approach before, but is this the correct way?

Was it helpful?

Solution

Yes. One needs to store data that can be asserted on in your @Then methods. The simplest way to do this is to have a member variable - but that means that your @When/@Then need to be in the same Steps class. Another way to do it is to have a shared data object that all your Steps use and you can then set it in one method and get it in another. If you just want something generic, you can do a Map<String,? extends Object> as your generic data object. And then if you run with multiple threads, then wrap the data object in a ThreadLocal.

That's what I've seen - and the data object should be setup/cleared with a @BeforeScenario/@AfterScenario method.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top