Controller 单元测试
在测试类中包含这三个注释,看起表面意思不难理解他们的作用,主要理解ContextConfiguration的使用。
@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration //默认是src/main/webapp
@ContextConfiguration("file:src/main/webapp/WEB-INF/spring/appServlet/servlet-context.xml")
注意:这里的@ContextConfiguration只解析了servlet-context.xml,如果项目中还存在其他模块的applicationContext,也需要把他们引进来否则得到的Service就是null的。
例如
@ContextConfiguration({
"file:src/main/webapp/WEB-INF/spring/appServlet/servlet-context.xml",
“classpath*: springxml/**.xml”
})
https://github.com/spring-projects/spring-mvc-showcase
Spring MVC Controller单元测试详解

本文深入探讨Spring MVC中Controller单元测试的实现方法,重点介绍了@RunWith, @WebAppConfiguration, @ContextConfiguration注解的作用及用法,并通过实例演示如何正确引入多个配置文件以确保Service的正常工作。
834

被折叠的 条评论
为什么被折叠?



