---
title: "spring controller层测试"
author: "yng02yng"
date: "2012-01-11T12:01:23Z"
tags: []
categories: ["非技术"]
slug: "spring-controller"
draft: false
lastmod: "2012-01-11T12:01:23Z"
id: 1340971
url: "https://yng02yng.iteye.com/blog/1340971"
---

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