Expert Spring MVC and Web Flow

(Dana P.) #1

Testing Spring


MVC Applications


By writing applications that are modular, pluggable, and loosely decoupled you are also cre-
ating applications that are extremely testable. The Spring Framework encourages you to build
applications in such a way that creating both unit tests and integration tests is fast, easy, and
rewarding. In this chapter, we will look at strategies and techniques for writing tests (both unit
and integration) for your Spring MVC components.
We will build upon the JUnit testing framework and use Spring’s built-in testing stubs
(found in spring-mock.jar), as well as introduce mock objects (with the jMock library) for use
with integration tests. One of the main selling points for building applications the Spring way
is that tests become feasible to create, and we’ll show you how to do it in this chapter.


Overview


When we say testing, what do we mean exactly? By testing, we specifically mean both unit
tests and integration tests. These types of tests are focused on the actual methods of classes
and the interactions between software components, respectively. What we won’t cover is user
acceptance testing, which is testing performed by users interacting with the interface of the
application. We certainly aren’t diminishing the usefulness of user acceptance tests, but unit
and integration tests should locate most of the issues before they ever reach the users.


Unit Tests


A tremendous amount of literature is already available about unit tests, so we won’t rehash it
all here. However, we will spend time discussing what a unit test is—and isn’t—to contrast
it with an integration test.


■TipLooking for more information on unit testing? We’d like to recommend both JUnit Recipes
by J.B. Rainsberger and Scott Stirling (Manning Publications, 2004), and Pragmatic Unit Testing in Java
(The Pragmatic Programmer, 2003) by Andrew Hunt and David Thomas.


283

CHAPTER 10


■ ■ ■

Free download pdf