參考:http://sishuok.com/forum/posts/list/7981.html ; http://www.tuicool.com/articles/6nqeIbmhtml
用下面的這種方式,不須要啓動tomcat服務器。java
因爲很差的敘述,在這就只是簡單的描述了。web
首先實體類User:spring
public class User {
private int id;
private String name;
private int age;
private String email;tomcat
......get、set、toString方法省略.....服務器
}mvc
UserController的代碼以下:app
import java.io.UnsupportedEncodingException;jsp
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;post
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import com.mjduan.learnMockMvc.entity.User;
import com.mjduan.learnMockMvc.service.UserService;
import com.mjduan.learnMockMvc.util.outUtil.Out;
@Controller
@RequestMapping("/UserController")
public class UserController {
@Autowired
@Qualifier("UserService")
private UserService userService;
@RequestMapping("/login")
public ModelAndView login(HttpServletRequest request,
HttpServletResponse response) throws UnsupportedEncodingException{
request.setCharacterEncoding("UTF-8");
String name = request.getParameter("name").trim();
String password = request.getParameter("password").trim();
Out.println("接收到的name:"+name+"/ password:"+password);
ModelAndView modelAndView = new ModelAndView();
User user = this.userService.getUserById(10012);
modelAndView.addObject("user", user);
modelAndView.setViewName("/jsp/success.jsp");
return modelAndView;
}
}
以後就是對UserController進行單元測試的UserControllerTest:
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.MvcResult;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import com.mjduan.learnMockMvc.controller.UserController;
import com.mjduan.learnMockMvc.entity.User;
import com.mjduan.learnMockMvc.util.outUtil.Out;
@RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = {"classpath:SpringApplicationContext.xml"}) public class UserControllerTest { @Autowired ApplicationContext ctx; private MockMvc mockMvc; private UserController userController; @Before public void before(){ //從spring容器中得到UserController對象 userController = (UserController) ctx.getBean("userController"); //MockMvcBuilders.standaloneSetup模擬一個Mvc測試環境,經過build獲得一個MockMvc //獨立測試方式,不須要啓動tomcat服務器 mockMvc = MockMvcBuilders.standaloneSetup(userController).build(); } @Test public void testLogin(){ try { //經過post方法來模擬post請求方式 MvcResult mvcResult = mockMvc.perform(MockMvcRequestBuilders.post("/UserController/login") .param("name", "jack") //設置請求參數爲name=jack .param("password", "123456")) //設置請求參數爲password=123456 .andExpect(MockMvcResultMatchers.view().name("/jsp/success.jsp")) //指望返回的ModelAndView中的viewName是/jsp/success.jsp .andExpect(MockMvcResultMatchers.model().attributeExists("user")) //指望返回的ModelAndView中含有數據user(user是一個key) .andExpect(MockMvcResultMatchers.model().attributeDoesNotExist("list")) //指望返回的ModelAndView中沒有數據list .andReturn(); //請求執行完畢以後全部的結果保存在mvcResult中 User user = (User) mvcResult.getModelAndView().getModel().get("user"); Out.println("得打的user:"+user.toString()); //Assert.assertNotNull(object); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } }