`
yng02yng
  • 浏览: 9225 次
最近访客 更多访客>>
社区版块
存档分类
最新评论

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(); 
  } 
  } 
  
  
分享到:
评论

相关推荐

    Spring Boot从Controller层进行单元测试的实现

    主要介绍了Spring Boot从Controller层进行单元测试的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧

    spring单元测试

    通过java配置的方式搭建了一个基本的web项目,主要用于单元测试示例,使用了spring支持h2内存数据库,不需要额外安装数据库,可以测试controller层、service层

    SpringBoot Controller Post接口单元测试示例

    今天小编就为大家分享一篇关于SpringBoot Controller Post接口单元测试示例,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧

    Spring.3.x企业应用开发实战(完整版).part2

    16.7.3 使用Spring RestTemplate测试 16.7.4 使用Selenium测试 16.8 小结 第17章 实战案例开发 17.1 论坛案例概述 17.1.1 论坛整体功能结构 17.1.2 论坛用例描述 17.1.3 主要功能流程描述 17.2 系统设计 17.2.1 技术...

    在Spring Framework中编写MongoDb集成测试的工具套件(高分项目).zip

    它采用MVC(Model-View-Controller,模型-视图-控制器)的架构模式,将应用程序分为模型层、视图层和控制器层,提供了处理请求、渲染视图和管理流程的功能。 3. MyBatis框架:MyBatis是一个持久层框架,用于与数据库...

    Zest是一款基于Spring的易于使用的单元测试工具(高分毕设).zip

    它采用MVC(Model-View-Controller,模型-视图-控制器)的架构模式,将应用程序分为模型层、视图层和控制器层,提供了处理请求、渲染视图和管理流程的功能。 3. MyBatis框架:MyBatis是一个持久层框架,用于与数据库...

    高级Spring Boot和Spring Cloud微服务示例 Zuul上的API Swagger2

    它采用MVC(Model-View-Controller,模型-视图-控制器)的架构模式,将应用程序分为模型层、视图层和控制器层,提供了处理请求、渲染视图和管理流程的功能。 3. MyBatis框架:MyBatis是一个持久层框架,用于与数据库...

    Spring Boot ( Jetty )、Spring Cloud库、Netflix OSS工具、Docker

    它采用MVC(Model-View-Controller,模型-视图-控制器)的架构模式,将应用程序分为模型层、视图层和控制器层,提供了处理请求、渲染视图和管理流程的功能。 3. MyBatis框架:MyBatis是一个持久层框架,用于与数据库...

    Spring-Reference_zh_CN(Spring中文参考手册)

    Controller 代码 14.6.2.3. Excel视图子类 14.6.2.4. PDF视图子类 14.7. JasperReports 14.7.1. 依赖的资源 14.7.2. 配置 14.7.2.1. 配置ViewResolver 14.7.2.2. 配置View 14.7.2.3. 关于报表文件 14.7.2.4. 使用 ...

    使用Spring in Guice和Guice in Spring的工具(高分项目).zip

    它采用MVC(Model-View-Controller,模型-视图-控制器)的架构模式,将应用程序分为模型层、视图层和控制器层,提供了处理请求、渲染视图和管理流程的功能。 3. MyBatis框架:MyBatis是一个持久层框架,用于与数据库...

    java、spring-boot、spring-tool-suite、maven、restful-api

    它采用MVC(Model-View-Controller,模型-视图-控制器)的架构模式,将应用程序分为模型层、视图层和控制器层,提供了处理请求、渲染视图和管理流程的功能。 3. MyBatis框架:MyBatis是一个持久层框架,用于与数据库...

    Spring3.x企业应用开发实战(完整版) part1

    16.7.3 使用Spring RestTemplate测试 16.7.4 使用Selenium测试 16.8 小结 第17章 实战案例开发 17.1 论坛案例概述 17.1.1 论坛整体功能结构 17.1.2 论坛用例描述 17.1.3 主要功能流程描述 17.2 系统设计 17.2.1 技术...

    用于将所有Spring xml配置转换为基于Spring java的配置的工具(高分毕设).zip

    它采用MVC(Model-View-Controller,模型-视图-控制器)的架构模式,将应用程序分为模型层、视图层和控制器层,提供了处理请求、渲染视图和管理流程的功能。 3. MyBatis框架:MyBatis是一个持久层框架,用于与数据库...

    Spring Boot Migrator (SBM)是一种用于自动代码迁移以升级或迁移到Spring Boot的工具

    它采用MVC(Model-View-Controller,模型-视图-控制器)的架构模式,将应用程序分为模型层、视图层和控制器层,提供了处理请求、渲染视图和管理流程的功能。 3. MyBatis框架:MyBatis是一个持久层框架,用于与数据库...

    基于Spring Boot的SSMP整合案例

    Dao开发——整合MyBatisPlus,制作数据层测试类 Service开发——基于MyBatisPlus进行增量开发,制作业务层测试类 Controller开发——基于Restful开发,使用PostMan测试接口功能 Controller开发——前后端开发协议...

    Starter webapp Spring Boot + Angular 2

    它采用MVC(Model-View-Controller,模型-视图-控制器)的架构模式,将应用程序分为模型层、视图层和控制器层,提供了处理请求、渲染视图和管理流程的功能。 3. MyBatis框架:MyBatis是一个持久层框架,用于与数据库...

    Spring-generator一键生成数据库文件

    Spring-generator 是基于 javafx8 开发的图形界面 Spring 代码生成器,使用 Apache FreeMarker 作为代码文件的模板,用户可以一键将数据库中的表生成为任意风格的 .java 代码文件(比如经典的三层模型)。 Spring-...

    Spring 3.x 中文开发手册.pdf

    7、spring测试框架和2,3,4的结合 8、spring配置文件中namespace的事情,不感兴趣 9、非标准setter类也可以注入了。。。不感兴趣 10、支持Servlet3的某个东东,可以写程序直接启动webapp,而非web.xml,不感兴趣 11、...

    基于 Spring Cloud 组件构建的分布式服务架构

    它采用MVC(Model-View-Controller,模型-视图-控制器)的架构模式,将应用程序分为模型层、视图层和控制器层,提供了处理请求、渲染视图和管理流程的功能。 3. MyBatis框架:MyBatis是一个持久层框架,用于与数据库...

    Spring中文帮助文档

    使用@Controller定义一个控制器 13.12.3. 使用@RequestMapping映射请求 13.12.4. 使用@RequestParam绑定请求参数到方法参数 13.12.5. 使用@ModelAttribute提供一个从模型到数据的链接 13.12.6. 使用@...

Global site tag (gtag.js) - Google Analytics