Overview

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

FAQ

What is the main idea of Chapter 5 in _Test Driven_?The chapter shows that test-driving Java EE web components is practical, not impossible. It focuses on applying TDD to controllers and views in MVC web applications, using examples from Servlets, Spring MVC, JSP, Velocity, and Wicket.
How does MVC divide responsibilities in a Java EE web application?In MVC, the model handles business logic and data, the view handles presentation, and the controller handles user interaction by talking to the model and choosing the next view to render.
Why are controllers considered testable even though they work with HTTP and web APIs?Controllers can be tested by isolating their behavior and faking or mocking their dependencies, such as request and response objects or service interfaces. The chapter shows that this is manageable with ordinary unit tests.
How can a Servlet be unit-tested without running a full web container?By using fake or mock implementations of HttpServletRequest and HttpServletResponse. The test sets up request parameters, invokes the Servlet, and then checks the response output or redirect target.
Why does the chapter introduce an AuthenticationService interface for the login example?The interface separates authentication logic from the Servlet or controller. This keeps the web component focused on its own responsibility and makes the login behavior easier to test by substituting a fake authentication service.
How does Spring MVC make controller testing easier than raw Servlets?Spring MVC simplifies dependency injection and controller structure. Controllers return a ModelAndView instead of directly handling rendering, and Spring can inject dependencies through setters, making tests and wiring cleaner.
What part of a view can be tested automatically?The chapter distinguishes between visual appearance, which usually needs human judgment, and view logic, which can be tested automatically. Examples include verifying that specific fields, labels, or values are rendered correctly.
Why is JspTest used for testing JSP pages?JspTest provides a lightweight JSP engine that can compile and render JSPs in unit tests without deploying them to a full application server. This makes JSP testing faster and more practical.
How does Velocity differ from JSP in terms of testability?Velocity is lighter and not tied to the Java EE stack, so it can be rendered directly in tests with very little infrastructure. The chapter shows that you can render templates with a context and then assert on the produced output using standard parsing tools.
What makes Wicket especially test-friendly?Wicket was designed with testability in mind. Pages and components are represented as Java classes, and the built-in WicketTester lets you verify page rendering, component presence, and user interaction without a web container.

pro $24.99 per month

  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose one free eBook per month to keep
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime

lite $19.99 per month

  • access to all Manning books, including MEAPs!

team

5, 10 or 20 seats+ for your team - learn more


Close
choose your plan

team

monthly
annual
$49.99
$499.99
only $41.67 per month
  • five seats for your team
  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose another free product every time you renew
  • choose twelve free products per year
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime
  • renews annually, pause or cancel renewal anytime
  • Test Driven Test Driven ebook for free
Close
choose your plan

team

monthly
annual
$49.99
$499.99
only $41.67 per month
  • five seats for your team
  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose another free product every time you renew
  • choose twelve free products per year
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime
  • renews annually, pause or cancel renewal anytime
  • Test Driven Test Driven ebook for free