Testing Spring MVC
This section will mock the request and session scope beans with MockHttpServletRequest
and MockHttpSession
:
- Create a dynamic web project,
SpringWebTest
, add theSpring-mvc
jars to the classpath, and modify theweb.xml
file (stored under<project>/WebContent/WEB-INF/
) to enable SpringDispatcherServlet
as follows:<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"> <display-name>SpringWebTest</display-name> <servlet> <servlet-name>dispatcher</servlet-name> ...
Get Mockito for Spring now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.