Rest-Assured : A BDD fashion Web Service testing framework
In this article we are going see how to use Rest Assure to test a web service.
Webservice has become one of common implemented system now a days for web and mobile applications. Webservice makes applications distributed to develop and easy to fix. We can provide scalability features easily. So, it is became more common and popular.
To test a webservice, we can use tool (Soap UI, Postman) or a framework like Rest assured. I will try to put some examples here.
The Test Webservice URL : https://github.com/sarkershantonu/Bug-Storing-WS/releases
This is Spring boot webservice. You have download the release and run locally.
If you run the webservice, the exposed URLs that we need to test are
1.To see all bugs : GET : /table/bugs
2.To save a bug: POST : /table/bugs
3.To see a bug : GET : /table/bugs/{id}
4.To Update a Bug: PUT : /table/bugs/{id}
5.To Delete a Bug : DELETE : /table/bugs/{id}
The main article posted in my blog.
So, lets see some test cases from this blog.
View All Bugs :
@Test
public void testViewAll() {
given().when().get().then().assertThat().
statusCode(HttpStatus.SC_OK).
contentType(ContentType.JSON).
header("Content-Type", "application/json;charset=UTF-8").
time(lessThan(globalResponseTimeout));
}
What we are doing here?
=>Destination URL(GET ) = localhost:9100/table/bugs
=>We are checking Http status, response type, content header, time.
3. Update a bug :
@Test
public void testUpdateeABug(){
Bug createdbug =
given().
contentType(ContentType.JSON).
body(Bug.getABug(),ObjectMapperType.JACKSON_2).
post().as(Bug.class);
createdbug.setTitle("This is modified title");
createdbug.setDescription("This is modified description");
given().
contentType(ContentType.JSON).
body(createdbug,ObjectMapperType.JACKSON_2).
when().
put(createdbug.getId().toString()).
then().
assertThat().
statusCode(HttpStatus.SC_ACCEPTED).
contentType(ContentType.JSON).
header("Content-Type", "application/json;charset=UTF-8").
body("id",equalTo(createdbug.getId().intValue()));
//cleanup
given().delete(createdbug.getId().toString()).
then().assertThat().statusCode(HttpStatus.SC_NO_CONTENT);
}
What we are doing here :
=>Destination URL(PUT) = localhost:9100/table/bugs/1
=>We are Creating a bug
=>From response, we get a bug and change its title & description.
=>We send update request for this the bug with same ID.
=>We are checking Http status, response type, content header, and response body should have id = same as requested bug's id
=>Finally we are deleting the bug & checking its status for cleaning up.
To see all tests, visit my blog : https://shantonusarker.blogspot.com/2016/12/rest-assured-bdd-fashion-web-service-testing-framework.html