Expert Spring MVC and Web Flow

(Dana P.) #1

Example


As always, code examples speak louder than words, so let’s create an example of a transactional
integration test. We will test the configuration and wiring of the typical components of a Spring
MVC application, including the Controller, service layer, and DAO.
For the example in Listing 10-14, we will create a test for the HomeControllerwe introduced
in Chapter 4.


Listing 10-14.HomeControllerIntegrationTest


public class HomeControllerIntegrationTest extends
AbstractTransactionalSpringContextTests {


private HomeController homeController;
private MockHttpServletRequest request;
private MockHttpServletResponse response;

public void setHomeController(HomeController homeController) {
this.homeController = homeController;
}

@Override
protected void onSetUpBeforeTransaction() throws Exception {
super.onSetUpBeforeTransaction();
request = new MockHttpServletRequest();
response = new MockHttpServletResponse();
}

@SuppressWarnings("unchecked")
public void testRequest() throws Exception {
request.setMethod("GET");
ModelAndView mav = homeController.handleRequest(request, response);
assertNotNull(mav);
List<SpecialDeal> specials = (List<SpecialDeal>)
mav.getModel().get("specials");
assertNotNull(specials);
assertTrue(specials.size() > 0);
}

@Override
protected String[] getConfigLocations() {
return new String[]{"classpath:applicationContext.xml",
"classpath:spring-servlet.xml"};
}
}


CHAPTER 10 ■TESTING SPRING MVC APPLICATIONS 305
Free download pdf