SSH框架下单元测试的实现

发表于:2018-10-24 11:53

字体: | 上一篇 | 下一篇 | 我要投稿

 作者:路过无痕飞飞    来源:51testing采编

分享:

  实现的功能
  实现了部门的增删改查
  对Action进行了单元测试
  对Service 进行了单元测试,通过mock的方式实现。
  实现的步骤
  一、对Action层的单元测试实现
  1、首先在pom文件中需要引入的依赖
  <dependency>
  <groupId>junit</groupId>
  <artifactId>junit</artifactId>
  <version>4.12</version>
  <scope>test</scope>
  </dependency>
  <dependency>
  <groupId>org.mockito</groupId>
  <artifactId>mockito-core</artifactId>
  <version>1.10.19</version>
  <scope>test</scope>
  </dependency>
  <dependency>
  <groupId>org.apache.struts</groupId>
  <artifactId>struts2-junit-plugin</artifactId>
  <version>2.1.8</version>
  <scope>test</scope>
  </dependency>
  <dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-test</artifactId>
  <version>3.2.8.RELEASE</version>
  </dependency>
  说明:
  在此处我们引入struts2-junit-plugin.2.1.8.jar,因为其里面有测试Struts2中action的类
  StrutsSpringTestCase,用来测试ssh中的action。
  2、新建一个测试类
  如果你使用的是idea,那么你可以直接在需要建立测试类的Action类中
  按ctrl+shift+t,新建一个测试类。如DepartmentActionTest
  3、如果是普通的action的话,
  待测试代码:
  public String findById() {
  String did = ServletActionContext.getRequest().getParameter("did");
  department=departmentService.findByDid(Integer.valueOf(did));
  return "goEditDepartment";
  }
  测试代码:
  @Test
  public void testFindById() throws Exception {
  /*这个函数相当@Before注解的函数,是调用单元测试后的时候,
  首先会执行的方法。可以在这里面做一些必要的准备工作*/
  request.setParameter("did", "1");
  ActionProxy proxy = getActionProxy("/department_findById.action");
  DepartmentAction action = (DepartmentAction) proxy.getAction();
  String result = action.findById();
  Assert.assertEquals("goEditDepartment", result);
  }
  4、如果是返回json的action的话,待测试代码:
  public void  findAll() {
  List<Department> departmentList= departmentService.findAll();
  JSONObject jsonObject = new JSONObject();
  if (CollectionUtils.isEmpty(departmentList)) {
  jsonObject.put("key", "success");
  jsonObject.put("data", JSON.toJSONString(departmentList));
  writeJson(jsonObject);
  return;
  }
  jsonObject.put("key", "success");
  jsonObject.put("data", JSON.toJSONString(departmentList));
  writeJson(jsonObject);
  }
  测试代码:
  String result = executeAction("/json_findAll.action");
  Assert.assertNotNull(result);
  JSONObject jsonObject = JSON.parseObject(result);
  if ("success".equals(jsonObject.getString("key"))) {
  List<Department> departmentList = JSON.parseArray(jsonObject.getString("data"), Department.class);
  if (CollectionUtils.isEmpty(departmentList)) {
  return;
  }
  for (Department department : departmentList) {
  System.out.println(department.toString());
  }
  }
  5、关于lazy问题的解决:首先在setUp()函数中加入下述代码:
  @Override
  protected void setUp() throws Exception {
  super.setUp();
  SessionFactory sessionFactory = lookupSessionFactory(request);
  Session hibernateSession= getSession(sessionFactory);
  TransactionSynchronizationManager.bindResource(sessionFactory,
  new SessionHolder(hibernateSession));
  //在只读模式下(FlushMode.NEVER/MANUAL)写操作不被允许
  hibernateSession.setFlushMode(FlushMode.AUTO);
  }
  然后在添加两个私有函数
  private Session getSession(SessionFactory sessionFactory) throws DataAccessResourceFailureException {
  Session session = sessionFactory.openSession();
  FlushMode flushMode = FlushMode.NEVER;
  if (flushMode != null) {
  session.setFlushMode(flushMode);
  }
  return session;
  }
  private SessionFactory lookupSessionFactory(HttpServletRequest request) {
  //“sessionFactory”是你spring配置文件(通常是application.xml)中的SessionFactory。
  //如:org.springframework.orm.hibernate4.annotation.AnnotationSessionFactoryBean
  return (SessionFactory)this.applicationContext.getBean("sessionFactory");
  }

    上文内容不用于商业目的,如涉及知识产权问题,请权利人联系博为峰小编(021-64471599-8017),我们将立即处理。
21/212>
《2023软件测试行业现状调查报告》独家发布~

关注51Testing

联系我们

快捷面板 站点地图 联系我们 广告服务 关于我们 站长统计 发展历程

法律顾问:上海兰迪律师事务所 项棋律师
版权所有 上海博为峰软件技术股份有限公司 Copyright©51testing.com 2003-2024
投诉及意见反馈:webmaster@51testing.com; 业务联系:service@51testing.com 021-64471599-8017

沪ICP备05003035号

沪公网安备 31010102002173号