Chapter 12. Testing web components

 

This chapter covers

  • Testing HTTP session data
  • Testing static and dynamic web pages
  • Testing JSP components
  • Testing servlet code
  • Testing dynamic data before it is displayed
  • Using ServletUnit to simulate the web container

In this chapter we provide recipes for testing web components in isolation, rather than testing the web application as a whole. We divided the chapters this way because we use different technologies and approaches to test a web application than we do to test its components—the servlets, JSPs, Velocity templates, and what have you that make up the application. In short, we test web applications from end to end using HtmlUnit, something described in detail in chapter 13, “Testing J2EE Applications,” but we use plain old JUnit and ServletUnit[1] to test web components in isolation—that is, without a container and, if we can, without invoking any business logic. How do we do it?

1 Part of the HttpUnit project (http://httpunit.sourceforge.net). Do not confuse this with the defunct project of the same name.

Test the components in a container

Simulate the container

Avoid the container

12.1. Test updating session data without a container

12.2. Test updating the HTTP session object

12.3. Test rendering a JavaServer Page

12.4. Test rendering a Velocity template

12.5. Test a JSP tag handler

12.6. Test your JSP tag library deployment

12.7. Test servlet initialization

12.8. Test the ServletContext

12.9. Test processing a request

12.10. Verify web page content without a web server

12.11. Verify web form attributes

12.12. Verify the data passed to a page template

12.13. Test a web resource filter