Chapter 5. Test-driving web components
Test-driving Java EE web components is presented as challenging in theory, but the chapter argues that it is quite manageable in practice. The discussion centers on the controller and view portions of MVC, since model code is already ordinary Java and therefore familiar territory for TDD. The chapter frames web development as a series of small, testable behaviors and emphasizes that the main obstacle is not the technology itself but learning how to isolate web components from their container and dependencies well enough to test them efficiently.
For controllers, the chapter walks through test-driving both plain Servlets and Spring MVC controllers. With Servlets, the key idea is to fake the request and response objects, then drive behavior such as redirects, parameter handling, and session state through tests. A login example shows how successive tests push the design toward better separation of concerns by extracting authentication into a dedicated service interface, which can be supplied as a fake in tests and later replaced with a real implementation. Spring controllers are shown to be even easier to test because dependency injection is built in, and the controller can simply return a ModelAndView object that the framework uses to select the view.
For views, the chapter distinguishes between visual appearance, which is often best checked manually, and view logic, which can and should be tested automatically. It shows how JavaServer Pages can be tested with lightweight infrastructure such as JspTest, including checking rendered form fields and page behavior like retaining a username after failed login. Velocity templates are presented as simpler still, because they can be rendered directly without a web container and verified through DOM and XPath assertions. Finally, the chapter shows that component-based frameworks such as Wicket can also be test-driven effectively, especially because Wicket offers testing support out of the box; overall, the chapter concludes that web components are very suitable for TDD when you work in small steps and keep the plumbing under control.
The division of responsibilities in the MVC architecture is straightforward. The lifecycle of a request begins with the controller component receiving an incoming HTTP request. The controller then delegates any business logic and data manipulation to the model components and obtains the necessary data from the model. In the third step, the controller populates the necessary data and domain objects for the response, finally delegating the rendering of the response to a view component.
Listing 5.1. Example Servlet echoing request parameters
Incoming HTTP requests are handled by the service() method of the HttpServlet base class from which our custom Servlets inherit. The service() method looks at the type of the HTTP request and subsequently dispatches the request to the matching doXXX() method.
Listing 5.2. Example unit test for the EchoServlet
Listing 5.3. First test for the LoginServlet
Listing 5.4. Our first cut at the LoginServlet (rejects any login attempt)
Listing 5.8. Valid login test rewritten to specify use of AuthenticationService
Listing 5.9. LoginServlet delegating authentication to a separate service interface
Listing 5.10. Configuring a LoginServlet with an AuthenticationService
Listing 5.12. Sample unit test for the LoginController
Mapping between LoginController and its dependency to an AuthenticationService implementation
Listing 5.14. Testing a valid login
Although it’s not practical to write tests for what the view looks like, it is most certainly possible to test the view logic. That is, it’s possible to test what the view does. In addition, although it’s typically not feasible to fully test the left side of the pie with automated tests, there are often some small aspects of the view that can be tested. For instance, we might want to have an automated test for checking that our web pages comply with the accessibility requirements of Section 508 (that is, those parts of Section 508 that don’t require human judgment—such as every image tag having a text equivalent defined with the alt attribute).
Listing 5.17. Our first cut at login.jsp
One possibility for test-driving JavaServer Pages is to deploy them into a lightweight, embeddable Servlet container and access them either using HTTP requests or through the container’s internal API.
Velocity’s basic concept is extremely simple. Velocity eats template text consisting of specialized markup for variable data and basic programming constructs, drinks some variable values on top, and spits out the rendered output.
Listing 5.21. Beginnings of a VelocityTestCase
Listing 5.22. Abstract base class provides plumbing to render Velocity templates
Listing 5.25. Example web.xml for a Wicket application
Listing 5.28. Our first WicketTester test
Listing 5.30. Testing for the presence of components
Test Driven ebook for free