参考: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(); } } }