Kontakt

Improved Testing Facilities

Posted on Dienstag, 16th November, 2010

Tapestry provides a very nice functionality which allows you to test your applications without any servlet container. The PageTester class is used to run a Tapestry application inside a test and render certain pages of the application. As shown in the following example, you can initialize a PageTester instance by providing:

  • the root package of the application. This is the same value you would specify using the tapestry.app-package context parameter inside web.xml.
  • Application name. This is the same value you would specify as the filter name inside web.xml.

The initialized PageTester instance may be used to render a page by its name. The return value of the renderPage() method is an instance of Document. A Document represents the root node of the DOM of the rendered page and may be used to perform asserts.

public class SimplePageTest {
   private PageTester tester;

   @BeforeClass
   public void setup() {
      tester = new PageTester(
            "org.example.testapp", "app");
    }

   @Test
   public void simple_test() {
      Document document = tester.renderPage("SimplePage");

      String markup = document.toString();

      assertTrue(markup.contains("Tapestry 5 Test"));
   }
}

This functionality allows you to test your pages with minimal effort but has a limitation: there is now way to test pages which don’t have a template. For example it is not possible to test the following page as there is no markup to assert against. So, PageTester fails to create an instance of Document and your tests fails.

public class SimplePage {

   Object onActivate() {
      return new HttpError(
         HttpServletResponse.SC_MOVED_TEMPORARILY,
         "Oups! Resource moved. Try again later.");
   }
}

Last week I committed some improvements on PageTester which provide you the full access to the response generated when the page was rendered. Instead of renderPage() method you can use the renderPageAndReturnResponse() method which returns an instance of TestableResponse. A TestableResponse is a testable extension of Response to perform asserts against. In the following example you can see how to assert the HTTP error sent by the page above.

public class SimplePageTest {

   @Test
   public void http_error) {
      TestableResponse response = tester
                .renderPageAndReturnResponse("SimplePage");

      assertEquals(
         response.getStatus(),
         HttpServletResponse.SC_MOVED_TEMPORARILY);

      assertEquals(
         response.getErrorMessage(),
         "Oups! Resource moved. Try again later.");
   }
}

You can also access the HTTP headers set during rendering the page or read the data written into ServletOutputStream.

public class SimplePageTest {

   @Test
   public void http_error() {
      TestableResponse response = tester
                .renderPageAndReturnResponse("AnotherPage");

      assertEquals(
         response.getHeader("my-header"),
         "foo");

      assertEquals(
         response.getOutput(),
         "<html><body>Bla bla</body></html>");
   }
}

For further details check out the classes PageTester and TestableResponse. There is more new stuff which will simplify testing of Tapestry applications.

Happy testing.

 

  1. Angelo
  2. Igor Drobiazko

 

avatar

Your name

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

 

Tapestry 5 Blog - Copyright © 2009 - Eclectic Theme by Your Inspiration Web - Powered by WordPress