Methods in controller classes will be mapped to the HTTP requests. Within the method, the request
will be processed, will bind to model objects, and will interact with the service layer (which was injected
into the controller classes via Spring's DI) to process the data. Upon completion, depending on the
result, the controller class will update the model and the view state (e.g., user messages, and so on) and
return the logical view (or the model with the view together) for Spring MVC to resolve the view to be
displayed to the user.
For unit testing controller classes, the main objective is to make sure that the controller methods
update the model and other view states properly and return the correct view. In addition, when an error
happens, testing ensures that the correct exception is thrown and the error messages are saved into the
state for displaying to the user. As we only want to test the controller classes' behavior, we need to
"mock" the service layer with the correct behavior.
For the ContactController class in the sample application, we would like to develop the test cases
for the list() and create() methods. In the following sections, we will discuss the steps for this,
including the development of some common infrastructure classes that support controller class testing
and the implementation of the test cases.
Implement the Infrastructure Classes
For a group of common tests (e.g., test cases for controller classes, service layer testing classes, and so
on), it's always a best practice to develop a common abstract parent class that has the mandatory testing
infrastructure set up correctly.
In this chapter, we will use Java classes for configuring Spring's TestContext. In addition, we will use
the profile feature in Spring 3.1 for configuring the components specific to the test environment.
Let's implement the Java class for Spring's ApplicationContext (the ControllerTestConfig class),
which is shown in Listing 19-1. Note that testing classes and resource files should be placed in the
folders /src/test/java and /src/test/resources, respectively.
Listing 19-1. The ControllerTestConfig Class
package com.apress.prospring3.ch19.test.config;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
@Configuration
@Profile("test")
public class ControllerTestConfig {
}
In Listing 19-1, we indicate to Spring that it's an ApplicationContext configuration class by applying
the @Configuration annotation to the class. Then, the @Profile annotation was applied to the class to
indicate the profile (in this case the test profile) that the beans configured in this class belong to.
We don't need any beans at the moment. However, it's always a good practice to maintain a
configuration class so that when the need arises in future (e.g., the Spring MVC layer needs to integrate
with an external FTP server), then the mocked bean for the FTP server can be defined in this
configuration class.
The next step is to implement the abstract base class for controller class test cases (the
AbstractControllerTest class), which is shown in Listing 19-2.
Search WWH :
Custom Search
Previous Page
Spring Framework 3 Topic Index
Next Page
Spring Framework 3 Bookmarks
Home