lunes, septiembre 24, 2012

Testing client side of RESTful services

People tell me A and B, They tell me how I have to see, Things that I have seen already clear, So they push me then from side to side (I Want Out - Helloween)
Develop an application that uses RESTful web API may imply developing server and client side. Writing integration tests for server side can be as easy as using Arquillian to start up server and REST-assured to test that the services works as expected. The problem is how to test the client side. In this post we are going to see how to test the client side apart from using mocks.

As a brief description, to test client part, what we need is a local server which can return recorded JSON responses. The rest-client-driver is a library which simulates a RESTful service. You can set expectations on the HTTP requests you want to receive during a test. So it is exactly what we need for our java client side. Note that this project is really helpful to write tests when we are developing RESTful web clients for connecting to services developed by third parties like Flickr Rest API, Jira Rest API, Github ...

First thing to do is adding rest-client-driver dependency:

Next step we are going to create a very simple Jersey application which simply invokes a get method to required URI.

And now we want to test that invokeGetMethod really gets the required resource. Let's suppose that this method in production code will be responsible of getting all issues name from a project registered on github.

Now we can start to write the test:

  • We use ClientDriverRule  @Rule annotation to add the client-driver to a test.
  • And then using methods provided by RestClientDriver class, expectations are recorded.
  • See how we are setting the base URL using driver.getBaseUrl()
With rest-client-driver we can also record http status response using giveEmptyResponse method:

And obviously we can record a put action:

Note that in this example, we are setting that our request should contain given message body to response a 204 status code.

This is a very simple example, but keep in mind that also works with libraries like gson or jackson. Also rest-driver project comes with a module that can be used to assert server responses (like REST-assured project) but this topic will be addressed into another post.

I wish you have found this post useful.

We keep learning,

2 comentarios:

Libin Huang dijo...

20160509libinralph lauren outlet
pandora outlet
tiffany outlet
nike huarache
ralph lauren polo
camisetas futbol baratas
basketball shoes,basketball sneakers,lebron james shoes,sports shoes,kobe bryant shoes,kobe sneakers,nike basketball shoes,running shoes,mens sport shoes,nike shoes
cheap nfl jersey
fitflops clearance
timberland boots
michael kors outlet
louis vuitton pas cher
rolex watches for sale
tory burch outlet
adidas wings
coach outlet
michael kors uk
juicy couture outlet
fitflops uk
ralph lauren polo shirts
longchamp solde
tory burch outlet
cazal outlet
toms shoes
ralph lauren outlet
true religion outlet
cazal sunglasses
swarovski outlet
longchamp handbags
michael kors outlet sale
oakley sunglasses
cheap football shirts
longchamp pas cher
adidas trainers

chenlina dijo...

coach outlet store online
jordan shoes
christian louboutin shoes
michael kors outlet
cheap jerseys
air jordan 13
cheap jordan shoes
jordan 13
coach outlet clearance
authentic louis vuitton handbags
louis vuitton outlet stores
louis vuitton outlet
nike roshe run
asics outlet
jordan 4 toro
louis vuitton
true religion
ray ban sunglasses outlet
coach outlet store online clearances
nike air max 90
nike outlet store
michael kors handbags
coach outlet
christian louboutin outlet
tory burch outlet online
oakley sunglasses
ralph lauren outlet
gucci outlet
nike air max
kobe 9
louis vuitton handbags
polo ralph lauren outlet
louboutin pas cher
ray ban sunglasses
louis vuitton handbags
true religion jeans
air jordan 8
asics shoes
louis vuitton outlet
nike sb shoes

Donate If You Can and Find Post Useful