1. 使用springmvc的单元测试类
类上标注的注解为:
①@RunWith(SpringJUnit4ClassRunner.class)
②@ContextConfiguration(locations = {"classpath:applicationContext.xml",
"file:src/main/webapp/WEB-INF/dispatcherServlet-servlet.xml"})
这个注解标注了spring和springmvc配置文件的位置
spring的配置文件在类路径下,springmvc的配置文件在文件路径下
2. 需要在类中传入spring的ioc容器,其中使用到@Autowired注解进行自动注入,因为是ioc容器本身,需要增加在类上增加另外一个注解@WebAppConfiguration使其能够正常注入
3. 创建MockMvc对象:
4. 写一个测试方法