问题描述:对Spring MVC项目中的Service层使用JUnit进行测试时出现错误 WebApplicationObjectSupport instance
[ResourceHttpRequestHandler [locations=[class path resource []], resolvers=
[org.springframework.web.servlet.resource.PathResourceResolver@3ce53f6a]]]
does not run in a WebApplicationContext but in: org.springframework.context.support.
GenericApplicationContext@54d901aa: startup date [Thu Mar 22 16:29:56 CST 2018]; root of context hierarchy
错误原因:@ContextConfiguration(locations = { "classpath*:/applicationContext.xml",
"classpath:/spring-mvc.xml"})出错,spring-mvc.xml不应该出现在这里
修正方式:将@ContextConfiguration(locations = { "classpath*:/applicationContext.xml","classpath:/spring-
mvc.xml"})改为@ContextConfiguration(locations = { "classpath*:/applicationContext.xml"})
备注:该文是我在学习JavaEE时遇到问题的备忘录,只针对我创建的工程遇到的问题!!!

本文解决了一个在SpringMVC项目中使用JUnit测试Service层时出现的配置错误问题。具体表现为测试环境中加载了spring-mvc.xml配置文件导致上下文启动失败。通过调整@ContextConfiguration注解设置,移除了spring-mvc.xml,成功解决了问题。

3万+

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



