Search code examples
javaresttddresteasyrest-client

How to TDD for Restful client code example


I did some TDDs before, but they were just straightforward and simple.

However, I will implement a restful client and invoke a restful API of third parties (Twitter, or Jira).

I used Resteasy client framework to implement that. The code is:

public void invokePUT() {
     ClientRequest request =
                 new ClientRequest("http://example.com/customers");
     request.accept("application/xml");
     ClientResponse<Customer> response = request.put(Customer.class); 
     try {
          if (response.getStatus() != 201)
               throw new RuntimeException("Failed!");
     } finally {
          response.releaseConnection(); 
     }}

If I want to write a test for this method (should write test before implement this method), what kind of the code should I write.

For GET, I can test the return Entity is equals to my expected entity and for POST, I can test the created entity's id is not null.

But how about for PUT and DELETE. Thanks.


Solution

  • Try to use REST Assured testing framework. It is great tool for testing REST services. On their website you'll find tons of examples how to use it. Just use it together with JUnit or TestNG to check assertions and you are done.