« - »

Testing Tools: DeleteMethodWebRequest

25 April 2009

Once I started to clone the GET method test to create similar tests for the PUT. POST, and DELETE methods, I discovered that there was a GetMethodWebRequest, a PutMethodWebRequest, and a PostMethodWebRequest, but no DeleteMethodWebRequest. That seemed a little odd to me, since all four are pretty standard methods in a REST environment, but it just wasn’t part of the package. I thought about creating my own, but I just had a hard time believing that I would be the first person to come along who needed such a thing.

With just a little help from our friends at Google, I quickly found out that I was, indeed, not the first person to have encountered this need, and I found just the code that I was looking for:

package org.restafarian.core.test.servlets;

import java.net.URL;

import com.meterware.httpunit.GetMethodWebRequest;

public class DeleteMethodWebRequest extends GetMethodWebRequest {

  /**
   * @param arg0
   */
  public DeleteMethodWebRequest(String arg0) {
    super(arg0);
  }

  /**
   * @param arg0
   * @param arg1
   */
  public DeleteMethodWebRequest(URL arg0, String arg1) {
    super(arg0, arg1);
  }

  /**
   * @param arg0
   * @param arg1
   * @param arg2
   */
  public DeleteMethodWebRequest(URL arg0, String arg1, String arg2) {
    super(arg0, arg1, arg2);
  }

  /**
   *
   * @see com.meterware.httpunit.GetMethodWebRequest#getMethod()
   */
  @Override
  public String getMethod() {
    return "DELETE";
  }
}

Basically, this just extends the existing GetMethodWebRequest and overrides the getMethod() method to return “DELETE” instead of “GET”. Not all that exciting, but it gets the job done.

With that out of the way, I was able to complete my cloning operation, and have tests in my new BaseServletTestCase for all four HTTP methods used by our servlets. Now it was time to attempt to implement a real servlet test and see how it all worked together.


http://blog.restafarian.org/2009/04/testing-tools-deletemethodwebrequest/

Leave a reply

You must be logged in to post a comment.