实现的功能
实现了部门的增删改查
对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),我们将立即处理。