问题描述
web.xml<?xml version="1.0" encoding="UTF-8"?><web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0" metadata-complete="true"> <display-name>lion web app</display-name> <context-param> <param-name>contextConfigLocation</param-name> <param-value> classpath:applicationContext-web.xml </param-value> </context-param> <!-- Bootsrtap the root application context as usual using ContextLoaderListener --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <filter> <filter-name>encodingFilter</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <init-param> <param-name>encoding</param-name> <param-value>UTF-8</param-value> </init-param> <init-param> <param-name>forceEncoding</param-name> <param-value>true</param-value> </init-param> </filter> <filter-mapping> <filter-name>encodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <servlet> <servlet-name>dispatcher</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextClass</param-name> <param-value> org.springframework.web.context.support.AnnotationConfigWebApplicationContext </param-value> </init-param> <init-param> <param-name>contextConfigLocation</param-name> <param-value> com.nexusy.lion.web.config.WebConfig com.nexusy.lion.web.config.FreeMarkerConfig </param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>dispatcher</servlet-name> <url-pattern>/*</url-pattern> </servlet-mapping> <session-config> <session-timeout>30</session-timeout> </session-config></web-app> 控制器@Controllerpublic class ExampleController { @RequestMapping("/index") public String welcome() { return "index"; }} 测试基类:@WebAppConfiguration@ContextConfiguration(classes={WebConfig.class, FreeMarkerConfig.class})public class WebAppTests { @Autowired protected WebApplicationContext wac;} 测试类:@RunWith(SpringJUnit4ClassRunner.class)public class ExampleControllerTests extends WebAppTests { private MockMvc mockMvc; @Before public void setUp() throws Exception { this.mockMvc = MockMvcBuilders.webAppContextSetup(wac).build(); } @Test public void testIndex() throws Exception { this.mockMvc.perform(get("/index")).andExpect(status().isOk()); }} 爆异常:javax.servlet.ServletException: Could not resolve view with name 'index' in servlet with name ''at org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1190)at org.springframework.test.web.servlet.TestDispatcherServlet.render(TestDispatcherServlet.java:112)at org.springframework.web.servlet.DispatcherServlet.processDispatchResult(DispatcherServlet.java:992)at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:939)at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:856)at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:936)at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:827)at javax.servlet.http.HttpServlet.service(HttpServlet.java:621)at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:812)at org.springframework.test.web.servlet.TestDispatcherServlet.service(TestDispatcherServlet.java:66)at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)at org.springframework.mock.web.MockFilterChain$ServletFilterProxy.doFilter(MockFilterChain.java:168)at org.springframework.mock.web.MockFilterChain.doFilter(MockFilterChain.java:136)at org.springframework.test.web.servlet.MockMvc.perform(MockMvc.java:134)at com.nexusy.lion.web.controller.ExampleControllerTests.testIndex(ExampleControllerTests.java:32)at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:74)at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:83)at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:72)at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:231)at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:88)at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:71)at org.junit.runners.ParentRunner.run(ParentRunner.java:309)at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:174)at org.junit.runner.JUnitCore.run(JUnitCore.java:160)at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:77)at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:195)at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:63)at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120) 如果视图改成jsp则可通过测试,Spring MVC Test Framework不支持freemarker吗?
解决方案
maven 多module时测试springmvc+freemarker的问题总结