---------------------------------------------------------------------------------------------------------------------------------------
情景:在做springMCV测试时出现该问题——执行simple方法
package com.springapp.mvc; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import org.springframework.test.context.web.WebAppConfiguration; import org.springframework.test.web.servlet.MockMvc; import org.springframework.web.context.WebApplicationContext; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.view; import static org.springframework.test.web.servlet.setup.MockMvcBuilders.webAppContextSetup; @RunWith(SpringJUnit4ClassRunner.class) @WebAppConfiguration @ContextConfiguration("file:src/main/webapp/WEB-INF/mvc-dispatcher-servlet.xml") public class AppTests { private MockMvc mockMvc; @SuppressWarnings("SpringJavaAutowiringInspection") @Autowired protected WebApplicationContext wac; @Before public void setup() { this.mockMvc = webAppContextSetup(this.wac).build(); } @Test public void simple() throws Exception { mockMvc.perform(post("/mytest")) .andExpect(view().name("hello")) .andDo(print()).andExpect(status().isOk()); } }
在此页面的网友也遇到笔者相同的问题,网友Sam Brannen的回答解决了此问题,下面贴出来:
蓝色框框中的内容即是此问题出现的原因及解决方法——原来Spring 4.0的版本中的org.springframework.mock.web包所需的servlet-api 的版本最低是3.0
解决过程:
1.下载servlet-api的3.0版本,下载地址:http://download.csdn.net/download/wangdongj2ee/4260894
2.按照我的博客http://www.cnblogs.com/wql025/p/4996484.html中的解决方法将下载的jar移至maven的本地仓库中。
pom.xml中的依赖代码如下:
<dependency> <groupId>javax.servlet</groupId> <artifactId>servlet-api</artifactId> <version>3.0</version> </dependency>
后记:茫茫人海,感谢网友在网络上的分享的智慧源泉。感谢!