spring controller层测试

spring controller层测试
2011年07月01日
   这是我最近写的测试类对应的方法的测试代码其下面,请大家提一些建议,看哪里写的不周到,谢谢,
  PositionController postController; Controller对象
  第一个方法:
  public String allPositions(HttpServletRequest request, ModelMap model) {
  TableFacade tableFacade = TableFacadeFactory.createSpringTableFacade("allPos itionsTable", request);
  tableFacade.setMaxRows(10);
  Limit limit = tableFacade.getLimit();
  FilterSet filterSet = limit.getFilterSet();
  int totalRows = projectService.getTotalRowCountForPositions(filter Set);
  tableFacade.setTotalRows(totalRows);
  SortSet sortSet = limit.getSortSet();
  int rowStart = limit.getRowSelect() == null? 0 : limit.getRowSelect().getRowStart();
  int rowEnd = limit.getRowSelect() == null? 10 : limit.getRowSelect().getRowEnd();
  List positions = projectService.getPositions(filterSet, sortSet, rowStart, rowEnd);
  model.addAttribute("positions", positions);
  model.addAttribute("limit", limit);
  return "/core/positions_table";
  }
  它对应的测试程序如下:
  @Test
  public void testAllPositions(){
  String view = postController.allPositions(request, model);
  Limit limit = (Limit)model.get("limit");
  List positions = (List)model.get("positions");
  Assert.assertNotNull(model.get("positions"));
  Assert.assertEquals(10, positions.size());
  Assert.assertNotNull(model.get("limit"));
  Assert.assertEquals(0, limit.getRowSelect().getRowStart());
  Assert.assertEquals(10, limit.getRowSelect().getRowEnd());
  Assert.assertTrue(view.contains("/core/positions_t able"));
  }
  第二个方法:
  public String getAllPosition(HttpServletRequest request,
  HttpServletResponse response,
  ModelMap model) {
  List positions = this.positionService.getAllPositions();
  //define the number of max rows per page for jmesa when the result is empty
  Integer maxRows = 10;
  if (CollectionUtils.isNotEmpty(positions)) {
  maxRows = positions.size();
  }
  model.addAttribute("count", maxRows);
  request.getSession().setAttribute("positions",posi tions);
  return "/crt/position_table";
  }
  它对应的测试程序如下:
  @Test
  public void testGetAllPosition(){
  String view = postController.getAllPosition(request, response, model);
  List positions = (List)request.getSession().getAttribute("p ositions");
  int maxRow = (Integer)model.get("count");
  Assert.assertNotNull(positions);
  Assert.assertEquals(maxRow, positions.size());
  Assert.assertTrue(view.contains("/crt/position_tab le"));
  }
  第三个方法:
  public void getBindPositionRolesInAllRoles(Integer positionId, HttpServletRequest request, HttpServletResponse response, ModelMap model) {
  Map> groupRoles = new HashMap>();
  try {
  groupRoles = positionService.getBindPositionRoleInAllRole(posit ionId);
  } catch (Exception e) {
  MessageUtils.outputJSONResult("occurError", response);
  return;
  }
  MessageUtils.outputJSONResult(JSONObject.fromObjec t(groupRoles).toString(), response);
  }
  它对应的测试程序如下:
  @Test
  public void testGetBindPositionRolesInAllRoles(){
  postController.getBindPositionRolesInAllRoles(1, request, response, model);
  try {
  Assert.assertEquals("utf-8", response.getCharacterEncoding());
  Assert.assertNotNull(response.getContentAsString() );
  //just the content whether is json format
  boolean flag = false;
  try{
  JSONObject.fromObject(JSONObject.fromObject(respon se.getContentAsString()));
  flag = true;
  }catch (Exception e){
  flag = false;
  }
  Assert.assertEquals(true, flag);
  } catch (UnsupportedEncodingException e) {
  e.printStackTrace();
  }
  }
  工具类MessageUtils的方法:
  public static void outputJSONResult(String result, HttpServletResponse response) {
  try {
  response.setHeader("ContentType", "text/json");
  response.setCharacterEncoding("utf-8");
  PrintWriter pw = response.getWriter();
  pw.write(result);
  pw.flush();
  pw.close();
  } catch (IOException e) {
  e.printStackTrace();
  }
  }
  
  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值