Basics of when(), given(), then(), and(), expect(), etc.

733 views
Skip to first unread message

Monty Zukowski

unread,
Jul 4, 2014, 11:36:43 PM7/4/14
to rest-a...@googlegroups.com
I've seen plenty of example code but I still don't know the appropriate conditions for using when(), given(), expect(), and(), then(), etc.  I can't seem to find a discussion in the documentation, have I missed it?

Johan Haleby

unread,
Jul 5, 2014, 3:20:33 AM7/5/14
to rest-a...@googlegroups.com
Hi, 

It's mainly semantics. Prior to REST Assured 2.0 it didn't support "given / when / then" which is more or less standard when you're doing some kind of BDD-like testing. Prior to 2.0 you probably did "given / expect / then" (which still works fine) but I think that "given / when / then" reads better and is easier to understand. There's one benefit with the "g/e/t" approach and that is that ALL expectation errors can be displayed at the same time which is not possible with the new syntax (since the expectations are defined last). 

"and()" is also just syntactic sugar as indicated by the Javadoc. It can add readability if you're writing everything in a one-liner, for example:

given().param("x", "y").and().header("z", "w").when().get("/something").then().assertThat().statusCode(200).and().body("x.y", equalTo("z"));

Regards,
/Johan


On Sat, Jul 5, 2014 at 5:36 AM, Monty Zukowski <mo...@codetransform.com> wrote:
I've seen plenty of example code but I still don't know the appropriate conditions for using when(), given(), expect(), and(), then(), etc.  I can't seem to find a discussion in the documentation, have I missed it?

--
You received this message because you are subscribed to the Google Groups "REST assured" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rest-assured...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Monty Zukowski

unread,
Jul 5, 2014, 12:46:18 PM7/5/14
to rest-a...@googlegroups.com
Thanks, that's helpful.  You might consider having a brief explanation of that at the beginning of the user guide.

Monty

Johan Haleby

unread,
Aug 4, 2014, 9:06:15 AM8/4/14
to rest-a...@googlegroups.com
I've finally got around to add this to the docs. Have a look https://code.google.com/p/rest-assured/wiki/Usage#Note_on_syntax. Thanks for your suggestion.


--

Monty Zukowski

unread,
Aug 4, 2014, 11:58:11 AM8/4/14
to rest-a...@googlegroups.com
Thank you, that makes things clear for a beginner like myself.
> You received this message because you are subscribed to a topic in the
> Google Groups "REST assured" group.
> To unsubscribe from this topic, visit
> https://groups.google.com/d/topic/rest-assured/sdSTGE-VUEM/unsubscribe.
> To unsubscribe from this group and all its topics, send an email to
Reply all
Reply to author
Forward
0 new messages