Test-Driven Development Using StrutsTestCase

Test-Driven Development Using StrutsTestCase

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.

Prev  [1] [2] [3] [4] Next

Close    To Top
  • Prev Article-Java:
  • Next Article-Java:
  • Now: Tutorial for Web and Software Design > Java > Java EE > Java Content
    Photoshop Tutorial
     

    Special Effect

      3D Effect
      Photoshop Articles
    Programming Tutorial
     

    C/C++ Tutorial

      Visual Basic
      C# Tutorial
    Database Tutorial
     

    MySQL Tutorial

      MS SQL Tutorial
      Oracle Tutorial
    Geek Tutorial
     

    Blogging Tutorial

      RSS Tutorial
      Podcasting Tutorial
    Graphic Design Tutorial
      Coreldraw Tutorial
      Illustrator Tutorial
      3D Tutorials
    Webmaster Articles
     

    Domain Service

      Web Hosting
      Site Promotion
    Java Tutorial/ Articles
     

    Java Servlets

      JavaEE Tutorial
     

    JavaBeans Tutorial

    XML Tutorial/ Articles
     

    XML Style

      AJAX Tutorial
      XML Mobile
    Flash Tutorial/ Articles
     

    Flash Video

      Action Script
      Flash Articles
    OS Tutorial/ Articles
      Linux Tutorial
      Symbian Tutorial
      MacOS Tutorial
    Personal Tech
      Hardware Tutorial
      Software Tutorial
      Online Auction