9

I have never used JUnit or other testing frameworks. All i know is how to develop rest service. I recently saw REST assured framework to test REST api. But all the articles that i found looks like below. But i don't know how to pass request xml and how will i get response and when should i call this method.?

Do i need to use some other tool before this REST assured.? I am completely beginner in this kind of testing frameworks. Please show me some light in this world. All i know is how to send request and check values in the response in SOAPUI. I have never tried this.

expect().
    statusCode(200).
    body(
      "user.email", equalTo("test@hascode.com"),
      "user.firstName", equalTo("Tim"),
      "user.lastName", equalTo("Testerman"),
      "user.id", equalTo("1")).
    when().
    get("/service/single-user/xml");
Manoj
  • 5,707
  • 19
  • 56
  • 86

1 Answers1

10

expect() /* what u expect after sending a request to REST Service */

statusCode(200) /*you are expecting 200 as statuscode which tells request handled successfully at server */

body() /* the conditions given in body are compare the value with expected values. "equalTo" hamcrest matcher condition (you need to have hamcrest jar in java classpath).*/

when(). /* as is name says above all will be done after sending get/post/put/delete request right so before you put these get,post,put,delete you will have this method as prefix */

get("/service/single-user/xml") /* the actual REST API request url goes here. can be GET/POST/PUT/DELETE. the confusion for you is its only showing half part which is base path.you can give entire request url in get() method.*/

more on: http://rest-assured.googlecode.com/svn/tags/1.8.1/apidocs/com/jayway/restassured/RestAssured.html

I hope this helps.

dileepvarma
  • 508
  • 2
  • 7
  • 30
  • Actually, when we POST request, we will send some xml format let say, my question is where to send that XML request. WebResource webResource = client .resource("http://localhost:8080/RESTfulExample/rest/json/metallica/post"); String input = "{\"singer\":\"Metallica\",\"title\":\"Fade To Black\"}"; ClientResponse response = webResource.type("application/json") .post(ClientResponse.class, input); In the sample snippet above, we are passing input object and getting response back. Like that how should i do in REST Assured..? How do we need to provide input obj. – Manoj Nov 20 '13 at 04:15
  • you can provide under given() method as below given().contentType("application/json").body("{\"singer\":\"Metallica\",\"title\":\"Fade To Black\"}").expect().statuscode(200).when().post("http://localhost:8080/RESTfulExample/rest/json/metallica/post"); – dileepvarma Nov 20 '13 at 06:28
  • Can anyone please provide one PUT example for http://www.thomas-bayer.com/sqlrest/CUSTOMER/ API, I am trying to add customer entry but it is always failing... my code looks like this -- @Test public void addSingleUser() { /*expect(). statusCode(400).*/ given(). parameters("ID", "13", "FIRSTNAME", "POL", "LASTNAME", "CRI", "STREET", "1 Main St.", "CITY", "CAD"). when(). put("http://www.thomas-bayer.com/sqlrest/CUSTOMER/13"). then(). body("CUSTOMER.ID", equalTo("13")); } – Paresh Apr 06 '15 at 03:32
  • are u able to do it with any rest client? what error ur getting – dileepvarma Apr 09 '15 at 10:54
  • I think I am ok for now, looks like service has some issue.. I tried with other service and it is working fine. thx! – Paresh Apr 12 '15 at 06:34