2019独角兽企业重金招聘Python工程师标准>>>
参考:http://sishuok.com/forum/posts/list/7981.html ; http://www.tuicool.com/articles/6nqeIbm
用下面的这种方式,不需要启动tomcat服务器。
由于不好的叙述,在这就只是简单的描述了。
首先实体类User:
public class User {
private int id;
private String name;
private int age;
private String email;
......get、set、toString方法省略.....
}
UserController的代码如下:
import java.io.UnsupportedEncodingException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
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();
}
}
}