views:

36

answers:

2

I'm trying to unit test a java WFS web service implementation. The service can accept requests containing KVP params such as: http://www.someserver.com/wfs&SERVICE=WFS&VERSION=1.1.0&REQUEST=GetFeature&TYPENAME=InWaterA_1M

or it can also accept a request containing an XML fragment such as

<?xml version="1.0" ?> 
  <GetFeature  version="1.1.0" service="WFS" maxFeatures="10000" 
       xmlns="http://www.opengis.net/wfs" 
       xmlns:myns="http://www.someserver.com/myns" 
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
        xsi:schemaLocation="http://www.opengis.net/wfs ../wfs/1.1.0/WFS.xsd"> 
    <Query typeName="myns:InWaterA_1M"/> 
  </GetFeature>

I'm testing the KVP way using ServletUnit, which is straight forward:

 ServletUnitClient sc = servletRunner.newClient();
 WebRequest request = new PostMethodWebRequest( "http://www.someserver.com/wfs
 request.setParameter( "SERVICE", "WFS );
 ...
 request.setParameter( "TYPENAME" "InWaterA_1M" ); 
 sc.getResponse( request);

I can't figure out how to create a corresponding request for the XML type of request though. Any ideas? I'd rather not have to use another testing framework library unless absolutely necessary.

A: 

From a quick look at the docs, it seems that ServletUnitClient can support POST requests, as well as GET requests with KVP style arguments, like you are using now: http://httpunit.sourceforge.net/doc/tutorial/task1editor-form.html

Request with XML works like posting an HTML form, only that you don't necessarily have the HTML UI in front of it.

However, I would probably break out the unit testing of the XML parsing to a separate test, and not test it explicitly through the servlet. The servlet is acting as an HTTP frontend for the XML parsing and other parts of the WFS service, and you should unit test those parts separately (perhaps you're already doing that, disregard this part in that case).

In my experience, testing the HTTP/frontend of a service is usually the least important part, the logic behind it is much more likely to break, and therefore more important to test. Also, testing the service logic separately from the frontend often forces you to use a better design.

Of course, if you have to the time, or the frontend itself involves a lot of logic, you should unit test that part as well.

Liedman
+1  A: 

You can create a do the following:

  1. Create a XML of the request you want..
  2. Create a MockHttpServletRequest API: http://static.springsource.org/spring/docs/2.0.x/api/org/springframework/mock/web/MockHttpServletRequest.html

  3. Call serContent(xml); and setContentType("text/xml");

  4. Call your servlet method directly. e.g. someServlet(mockReq,mockRes);

This way there is no need to fire up the servlet container while jUnit testing...

Apache Fan