用mockito对DAO层进行单元测试

时间:2015-12-10 10:26:03

标签: java hibernate unit-testing spring-mvc mockito

我正在尝试使用Mockito进行Spring + Hibernate项目的单元测试。以下是我的DAO类的实现:

@Repository
public class EmployeeDAOImpl implements EmployeeDAO {
    @Autowired
    private SessionFactory sessionFactory;

    public void setSessionFactory(SessionFactory sessionFactory) {
        this.sessionFactory = sessionFactory;
        System.out.println("Session factory");
    }
    @Override
    public ArrayList<Employees> getAllData() {
        System.out.println("Inside get all data");
        Session session = sessionFactory.getCurrentSession();
        System.out.println("session created");
        Criteria cr = session.createCriteria(Employees.class, "employees").setResultTransformer(
                Criteria.DISTINCT_ROOT_ENTITY);

        return (ArrayList<Employees>) cr.list();
    }

}

以下是我的测试课程:

public class EmployeeDAOImplTest {

    @Mock
    private SessionFactory sessionFactory;
    @Mock
    private Session session;
    @Mock
    Criteria criteria;
    @Spy
    ArrayList<Employees> employees = new ArrayList<Employees>();

    private EmployeeDAO dao;

    @Before
    public void setUp() throws Exception {
        MockitoAnnotations.initMocks(this);
        dao = new EmployeeDAOImpl();
        dao.setSessionFactory(sessionFactory);
        employees = getEmpDetails();
        Mockito.doReturn(session).when(sessionFactory).getCurrentSession();

    }

    @Test
    public void testGetAllData() {
        Mockito.doReturn(criteria).when(session)
                .createCriteria(ConceptModelDetails.class);
        Mockito.doReturn(employees).when(criteria).list();
            assertEquals(employees, dao.getAllData());
    }

    private ArrayList<Employees> getEmpDetails() {
        ArrayList<Employees> array = new ArrayList<Employees>();
        Employees emp = new Employees();
        emp.setName("xyz");
        emp.setId(12);
        array.add(cm);
        return array;
    }

}

employees是Employees的虚拟数组列表。请在这里建议他的问题。当我运行这个测试用例时,我在Null Pointer Exception中遇到了失败:

Criteria cr = session.createCriteria(Employees.class, "employees").setResultTransformer(
                    Criteria.DISTINCT_ROOT_ENTITY);

堆栈追踪:

java.lang.NullPointerException
    at com.assignment.dao.impl.EmployeeDAOImpl.getAllData(EmployeeDAOImpl.java:77)
    at com.assignment.DAOTest.EmployeeDAOImplTest.testGetAllData(EmployeeDAOImplTest.java:84)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
    at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:76)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:236)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)

1 个答案:

答案 0 :(得分:4)

EmployeeDAOImpl包含以下代码:

    Criteria cr = session.createCriteria(Employees.class, "employees").setResultTransformer(
            Criteria.DISTINCT_ROOT_ENTITY);

您的考试有:

    Mockito.doReturn(criteria).when(session)
            .createCriteria(ConceptModelDetails.class);

从代码中我可以看出,你正在嘲笑sessionFactory和它正在返回的session。但是,您没有正确地模拟session.createCriteria(Employee.class, "employees")(而是设置session.createCriteria(ConceptModelDetails.class)的返回值)。因此,模拟的返回值将为null。因此,当您立即取消引用它(调用setResultTransformer)时,您将获得NullPointerException

要验证这一点,请尝试单步执行调试器,或向EmployeeDAOImpl添加日志语句,以显示sessionFactorysession的值以及session.createCriteria(...)的结果