« - »

Testing Tools: LookupTableEntryJsonServletTest

26 April 2009

Continuing with my strategy of taking on small, incremental steps in this process of setting up a servlet testing framework, I cloned our successful LookupTableJsonServletTest to create a similar test case, the new LookupTableEntryJsonServletTest. The major difference between the LookupTableJsonServlet and the LookupTableEntryJsonServlet is that the LookupTableJsonServlet returns data for all tables and the LookupTableEntryJsonServlet returns data for a specific table, and therefore, requires a URL that identifies the table in question. This means that the URL mapping ends in “/*”, which created issues for our default tests.

To resolve that problem, I had to modify the BaseServletTestCase to add an additional field to represent the mapping used for testing, which would have to be different from the mapping used to define the servlet. In our case, the mapping used to define the servlet was “options/*”, but the mapping used to test the servlet had to be “options”. The following code addition to the constructors took care of this problem:

if (servletMapping.indexOf("/*") != -1) {
  servletTestMapping = servletMapping.substring(0, servletMapping.indexOf("/*"));
} else {
  servletTestMapping = servletMapping;
}

Other than that little wrinkle, the new test case was pretty much a clone of the original, with a few more tests thrown in to accommodate the added complexity of having to pass in the context and the table name for the requested table:

package org.restafarian.core.servlets.impl;

import org.restafarian.core.test.servlets.BaseServletTestCase;
import org.restafarian.core.test.servlets.GenericSpringTestServlet;

import com.meterware.httpunit.WebResponse;

/**
 * <p>This class tests the LookupTableEntryJsonServlet.</p>
 */
public class LookupTableEntryJsonServletTest extends BaseServletTestCase<GenericSpringTestServlet> {

  /**
   * <p>Constructs a new LookupTableEntryJsonServletTest.</p>
   */
  public LookupTableEntryJsonServletTest() {
    super("lookupTableEntryJsonServlet", "options/*");
  }

  /**
   * <p>Tests the servlet's GET method.</p>
   */
  public void testServletGet() {
    try {
      // test with no context
      WebResponse webResponse = getGetMethodWebResponse(null, "/options");
      assertEquals("Unexpected response code from testing LookupTableEntryJsonServlet GET method",
           404, webResponse.getResponseCode());
      // test with context, but no table
      webResponse = getGetMethodWebResponse(null, "/options/test");
      assertEquals("Unexpected response code from testing LookupTableEntryJsonServlet GET method",
           404, webResponse.getResponseCode());
      // test with context and table, but no query string
      webResponse = getGetMethodWebResponse(null, "/options/test/test");
      assertEquals("Unexpected response code from testing LookupTableEntryJsonServlet GET method",
           200, webResponse.getResponseCode());
      assertTrue("Unexpected content returned from testing LookupTableEntryJsonServlet GET method: "
           + webResponse.getText(), webResponse.getText().trim().startsWith("["));
      assertTrue("Unexpected content returned from testing LookupTableEntryJsonServlet GET method: "
           + webResponse.getText(), webResponse.getText().trim().endsWith("]"));
      // test with context, table, and query string
      webResponse = getGetMethodWebResponse(null, "/options/test/test?var=entryOptions");
      assertEquals("Unexpected response code from testing LookupTableEntryJsonServlet GET method",
           200, webResponse.getResponseCode());
      assertTrue("Unexpected content returned from testing LookupTableEntryJsonServlet GET method: "
           + webResponse.getText(), webResponse.getText().trim().startsWith("var entryOptions = ["));
      assertTrue("Unexpected content returned from testing LookupTableEntryJsonServlet GET method: "
           + webResponse.getText(), webResponse.getText().trim().endsWith("];"));
    } catch (Exception e) {
      log.error("Error testing LookupTableEntryJsonServlet GET method; Exception is " + e, e);
      fail("Error testing LookupTableEntryJsonServlet GET method; Exception is " + e);
    }
  }
}

Next, it was just a matter of running it though its paces:

-------------------------------------------------------
 T E S T S
-------------------------------------------------------
Running org.restafarian.core.servlets.impl.LookupTableEntryJsonServletTest
INFO - AbstractSingleSpringContextTests.loadContextLocations(187) | Loading context for locations:
     /context/applicationContext*.xml
DEBUG - AbstractTransactionalSpringContextTests.startNewTransaction(392) | Began transaction (1):
     transaction manager [org.springframework.orm.hibernate3.HibernateTransactionManager@1469658];
     rollback [false].
Rhino classes (js.jar) not found - Javascript disabled
DEBUG - BaseServletTestCase.getGetMethodWebResponse(178) | Testing "options/*" with URL

http://localhost/options

DEBUG - LookupTableEntryJsonServlet.doGet(43) | Processing GET request; id=null
DEBUG - JavascriptServletBase.sendError(164) | Sending error 404; message=The requested resource
     was not found on this server.
DEBUG - BaseServletTestCase.getGetMethodWebResponse(178) | Testing "options/*" with URL

http://localhost/options/test

DEBUG - LookupTableEntryJsonServlet.doGet(43) | Processing GET request; id=test
DEBUG - JavascriptServletBase.sendError(164) | Sending error 404; message=The requested resource
     was not found on this server.
DEBUG - BaseServletTestCase.getGetMethodWebResponse(178) | Testing "options/*" with URL

http://localhost/options/test/test

DEBUG - LookupTableEntryJsonServlet.doGet(43) | Processing GET request; id=test/test
DEBUG - BaseServletTestCase.getGetMethodWebResponse(178) | Testing "options/*" with URL

http://localhost/options/test/test?var=entryOptions

DEBUG - LookupTableEntryJsonServlet.doGet(43) | Processing GET request; id=test/test
DEBUG - AbstractTransactionalSpringContextTests.endTransaction(356) | Committed transaction after
     execution of test [testServletGet].
DEBUG - AbstractTransactionalSpringContextTests.startNewTransaction(392) | Began transaction (1):
     transaction manager [org.springframework.orm.hibernate3.HibernateTransactionManager@1469658];
     rollback [false].
DEBUG - BaseServletTestCase.getPutMethodWebResponse(228) | Testing "options/*" with URL

http://localhost/options

DEBUG - AbstractTransactionalSpringContextTests.endTransaction(356) | Committed transaction after
     execution of test [testServletPut].
DEBUG - AbstractTransactionalSpringContextTests.startNewTransaction(392) | Began transaction (1):
     transaction manager [org.springframework.orm.hibernate3.HibernateTransactionManager@1469658];
     rollback [false].
DEBUG - BaseServletTestCase.getPostMethodWebResponse(279) | Testing "options/*" with URL

http://localhost/options

DEBUG - AbstractTransactionalSpringContextTests.endTransaction(356) | Committed transaction after
     execution of test [testServletPost].
DEBUG - AbstractTransactionalSpringContextTests.startNewTransaction(392) | Began transaction (1):
     transaction manager [org.springframework.orm.hibernate3.HibernateTransactionManager@1469658];
     rollback [false].
DEBUG - BaseServletTestCase.getDeleteMethodWebResponse(329) | Testing "options/*" with URL

http://localhost/options

DEBUG - AbstractTransactionalSpringContextTests.endTransaction(356) | Committed transaction after
     execution of test [testServletDelete].
Tests run: 4, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 10.329 sec

Now it’s time to look for something a just a little more complicated as we slowly ramp up the capabilities of our base test class. I’ll hunt around through the existing servlets and see what looks good for our next candidate.


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

Leave a reply

You must be logged in to post a comment.