所以这是一个非常基本的 spring-boot 项目,只有 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)
我相信这是因为SpringApplicationContextLoader
line 255
, 方法通过调用只接受路径的构造函数来addMockServletContext
创建 a 。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
,对吗?