ONJava.com -- The Independent Source for Enterprise Java
oreilly.comSafari Books Online.Conferences.

advertisement

AddThis Social Bookmark Button

Test-Driven Development Using StrutsTestCase
Pages: 1, 2, 3, 4

In practice, we will probably want to perform business-specific tests on the test results. For instance, suppose the results attribute is expected to be a List containing a list of exactly 100 Hotel domain objects, and that we want to be sure that all of the hotels in this list are in France. To do this type of test, the code will be very similar to standard JUnit testing:




public void testSearchByCountry() {
  setRequestPathInfo("/search.do");
  addRequestParameter("country", "FR");
  actionPerform();
  verifyNoActionErrors();
  verifyForward("success");
  assertNotNull(request.getAttribute("results"));
  List results 
    = (List) request.getAttribute("results"); 
  assertEquals(results.size(), 100);
  for (Iterator iter = results.iterator(); 
       iter.hasNext();) {
       Hotel hotel = (Hotel) iter.next();
       assertEquals(hotel.getCountry, 
                    TestConstants.FRANCE);
       ...
  }
}

When you test more complex cases, you may want to test sequences of actions. For example, suppose the user does a search on all hotels in France, and then clicks on an entry to display the details. Suppose we have a Struts action to display the details of a given hotel, which can be called as follows:

/displayDetails.do?id=123456

Using StrutsTestCase, we can easily simulate a sequence of actions in the same test case, where a user performs a search on all hotels in France, and then clicks on one to see the details:


public void testSearchAndDisplay() {
  setRequestPathInfo("/search.do");
  addRequestParameter("country", "FR");
  actionPerform();
  verifyNoActionErrors();
  verifyForward("success");
  assertNotNull(request.getAttribute("results"));
  List results 
       = (List) request.getAttribute("results"); 
  assertEquals(results.size(),100);
  Hotel hotel = (Hotel) results.get(0);

  setRequestPathInfo("/displayDetails.do");
  addRequestParameter("id", hotel.getId());
  actionPerform();            
  verifyNoActionErrors();
  verifyForward("success");
  Hotel hotel 
        = (Hotel)request.getAttribute("hotel");
  assertNotNull(hotel);
  ...
}

Testing Struts Error Handling

It is also important to test error handling. Suppose we want to check that the application behaves gracefully if an illegal country code is specified. We write a new test method and check the returned Struts ErrorMessages using verifyActionErrors():


public void testSearchByInvalidCountry() {
  setRequestPathInfo("/search.do");
  addRequestParameter("country", "XX");
  actionPerform();
  verifyActionErrors(
      new String[] {"error.unknown,country"});
  verifyForward("failure");
}

Sometimes you want to verify data directly in the ActionForm object. You can do this using getActionForm(), as in the following example:


public void testSearchByInvalidCountry() {
  setRequestPathInfo("/search.do");
  addRequestParameter("country", "XX");
  actionPerform();
  verifyActionErrors(
      new String[] {"error.unknown,country"});
  verifyForward("failure");
  SearchForm form = (SearchForm) getActionForm();
  assertEquals("Scott", form.getCountry("XX"));        
}

Here, we verify that the illegal country code is correctly kept in the ActionForm after an error.

Pages: 1, 2, 3, 4

Next Pagearrow