Wednesday, January 4, 2012

A way to test REST resource

While looking around for an easy way to test REST APIs, I stumbled across rest-assured
This is a very simple and elegant way to test your code.

Add the following to your pom.xml
 <dependency>  
     <groupId>com.jayway.restassured</groupId>  
     <artifactId>rest-assured</artifactId>  
     <version>1.5</version>  
     <scope>test</scope>  
 </dependency>  

Your test will look something like ..
 public void testInvalidInputInvalidDelayTol()  
 {  
  with().  
   parameters(.....).  
  expect().  
   statusCode(400).  
   body(  
    equalToIgnoringWhiteSpace("....")  
   ).  
  when().  
   with().authentication().preemptive().basic("username", "password").  
   get("/network-adapter/network");  
 }  


Rest-assured supports various response formats like json, xml.  Explore the rest-assured wiki to learn more.


No comments:

Post a Comment