what is returned when I use methods like given() and when()? And other clarifying questions

1,203 views
Skip to first unread message

Jeffrey Ji

unread,
Jun 16, 2016, 11:25:28 PM6/16/16
to REST assured
Sorry if these are noob questions, I've only been using Java and Rest-Assured for the past few days, so my understanding of the underlying Java might not be very solid (I come from a background of Python and C++, still in University).

Is there already a global RestAssured object when I compile, or am I supposed to create my own? AKA, which should I be doing?

RestAssured.baseURI = "https://some_website.com";
//modifies some global object that lives the entire duration of the code lifetime

OR

Request foo = new Response;
foo.baseURI = "https://some_website.com";
//Now that I think about it, this probably is what actually happens...

Do all the methods modify the original object, or do they return a modified copy? (Do I need to keep reassigning a response object back to itself when I call a method on it?)

Request foo;
foo = foo.given().header("headerName1", "headerValue1");

OR

foo.given().header("headerName1", "headerValue1"); // modifies the original?

What exactly do the methods given() and when() and then() do? Do they return a different object type? What I think might be happening is this:

auto foo = RestAssured.port(80); //port() returns a request object
                                 //any method called on foo at this point
                                 //returns a copy of the original request object modified in some way
                                 //(aka changing the default port or URI)

auto foo2 = foo.when().get(/login").param("paramName1", "paramValue1");
//As soon as when() method is invoked on foo, it turns into a response object (is this right?)
//now that foo2 is a response object, you can call methods like statuscode(200) on it, which will
//either throw an exception if the statuscode doesn't match 200, or return itself.

If my assumptions are correct above, then what exactly do the then(), given(), and when() methods do? Are they just syntactic sugar (first time I've encountered this term, actually)?
I assume it doesn't do anything, because after method get(), the returned object is already of class Response. Is there even a difference between Request and Response objects?
Or are they the same class type?

Todd Bradley

unread,
Jun 17, 2016, 12:10:59 AM6/17/16
to rest-a...@googlegroups.com
Hmm, where to start. First, I'd recommend getting familiar with a decent Java IDE. I use Eclipse, but IntelliJ is very popular. Then, when you write some of these examples, you can hover your cursor over the methods and the IDE will show you information about the methods, like what they return.

No, there isn't really a global RestAssured object, unless you mean the class itself (Java uses the terms "class" and "object" and "instance" the same way C++ does). There are a few static methods that allow you to do global configuration, but you don't need those most of the time.

given() is a static method (on the RestAssured class) that returns a RequestSpecification. More formally, you could call it RestAssured.given(), but everyone typically does a "import static com.jayway.restassured.RestAssured.given;" so we can just write the shorthand given() instead. Similarly, when() also returns a RequestSpecification.

I'm confused about your questions about Request objects. I'm not aware of REST-assured even having a Request class. Do you mean RequestSpecification?


Cheers,
Todd.



--
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.

Jeffrey Ji

unread,
Jun 17, 2016, 4:36:21 PM6/17/16
to REST assured
Todd,

Thanks for the reply. Right now, I am using Eclipse Keplar. When I said Request objects, I did mean RequestSpecification objects. I kind of get it now. So given() returns a standard RequestSpecification object, and I can call more methods on it to return my own version of a RequestSpecification object, like so:

RequestSpecification = given().port(40) //basically the standard object w/ port changed

I guess the design pattern is starting to make sense to me. Calling a method on a RequestSpecification argument allows me to test many variations of the same object. If I wanted to test my object with different ports, I could just do:

someFunction(myResponseSpecification.port(50));
someOtherFunc(myResponseSpecification.port(40));
Reply all
Reply to author
Forward
0 new messages