Spring Boot,MockMVC和MessageSource

时间:2015-02-17 19:21:40

标签: spring-boot mockmvc

所以这里是一个非常基本的春季启动项目,只有6个班级。

https://github.com/paranoiabla/spring-boot-mockmvc

设置非常简单,只有一个测试用例失败。我得到的例外是

org.springframework.web.util.NestedServletException: Request processing failed; nested exception is org.springframework.context.NoSuchMessageException: No message found under code 'custom.link' for locale 'en'.
at org.springframework.context.support.AbstractMessageSource.getMessage(AbstractMessageSource.java:159)
at org.test.MyController.home(MyController.java:29)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.springframework.web.bind.annotation.support.HandlerMethodInvoker.invokeHandlerMethod(HandlerMethodInvoker.java:177)
at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.invokeHandlerMethod(AnnotationMethodHandlerAdapter.java:446)
at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle(AnnotationMethodHandlerAdapter.java:434)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:943)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:877)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:966)
at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:857)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:618)
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:842)
at org.springframework.test.web.servlet.TestDispatcherServlet.service(TestDispatcherServlet.java:65)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:725)

我相信这是因为SpringApplicationContextLoader255,方法addMockServletContext通过调用只接受路径的构造函数来创建MockServletContext。因此,在MockServletContext内部ResourceLoader初始化为DefaultResourceLoader,这将在类路径中查找资源。这样,如果我有ReloadableResourceMessageResolver并且我的属性文件位于/WEB-INF/messages/base.properties,它将永远找不到它们,因为/WEB-INF/messages/base.properties被转换为src/main/webapp/WEB-INF/messages/base.properties,这在类路径中找不到ReloadableMessageSourceResolver。我认为在创建MockServletContext时应该能够明确传递FileSystemResourceLoader,对吧?

1 个答案:

答案 0 :(得分:0)

我猜WEN-INF应为WEB-INF

虽然我没有检查它解决了你的问题。