📄 employeemanagertest.java
字号:
package org.appfuse.service;import java.util.List;import java.util.ArrayList;import org.appfuse.service.BaseManagerTestCase;import org.appfuse.dao.EmployeeDao;import org.appfuse.model.Employee;import org.appfuse.service.impl.EmployeeManagerImpl;import org.jmock.Mock;import org.springframework.orm.ObjectRetrievalFailureException;public class EmployeeManagerTest extends BaseManagerTestCase { private final String employeeId = "1"; private EmployeeManagerImpl employeeManager = new EmployeeManagerImpl(); private Mock employeeDao = null; protected void setUp() throws Exception { super.setUp(); employeeDao = new Mock(EmployeeDao.class); employeeManager.setEmployeeDao((EmployeeDao) employeeDao.proxy()); } protected void tearDown() throws Exception { super.tearDown(); employeeManager = null; } public void testGetEmployees() throws Exception { List results = new ArrayList(); Employee employee = new Employee(); results.add(employee); // set expected behavior on dao employeeDao.expects(once()).method("getEmployees") .will(returnValue(results)); List employees = employeeManager.getEmployees(null); assertTrue(employees.size() == 1); employeeDao.verify(); } public void testGetEmployee() throws Exception { // set expected behavior on dao employeeDao.expects(once()).method("getEmployee") .will(returnValue(new Employee())); Employee employee = employeeManager.getEmployee(employeeId); assertTrue(employee != null); employeeDao.verify(); } public void testSaveEmployee() throws Exception { Employee employee = new Employee(); // set expected behavior on dao employeeDao.expects(once()).method("saveEmployee") .with(same(employee)).isVoid(); employeeManager.saveEmployee(employee); employeeDao.verify(); } public void testAddAndRemoveEmployee() throws Exception { Employee employee = new Employee(); // set required fields employee.setCode("OxEuUqPaOn"); employee.setDept("CvJzEcSpVlXnDbVaByJvPfAmNdYhFtTxVdKtVbYtTiBoFwMxXi"); employee.setName("HoJfFbNxHaEdXuVfEbRb"); employee.setStatus("BcUeVaHaFk"); employee.setTitle("OkIrMiQsBbFySxCrXcPmDnIbKgVhRhGkTlJcYjEaRkFyGgMzQz"); // set expected behavior on dao employeeDao.expects(once()).method("saveEmployee") .with(same(employee)).isVoid(); employeeManager.saveEmployee(employee); employeeDao.verify(); // reset expectations employeeDao.reset(); employeeDao.expects(once()).method("removeEmployee").with(eq(new Long(employeeId))); employeeManager.removeEmployee(employeeId); employeeDao.verify(); // reset expectations employeeDao.reset(); // remove Exception ex = new ObjectRetrievalFailureException(Employee.class, employee.getId()); employeeDao.expects(once()).method("removeEmployee").isVoid(); employeeDao.expects(once()).method("getEmployee").will(throwException(ex)); employeeManager.removeEmployee(employeeId); try { employeeManager.getEmployee(employeeId); fail("Employee with identifier '" + employeeId + "' found in database"); } catch (ObjectRetrievalFailureException e) { assertNotNull(e.getMessage()); } employeeDao.verify(); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -