---------------------------------------------------------------------------------------------------------------------------------------
情景:在做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>
后記:茫茫人海,感謝網友在網絡上的分享的智慧源泉。感謝!