java-使用Mockito对DAO层进行单元测试

我正在尝试使用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;
    }

}

employee是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)

解决方法:

EmployeeDAOImpl包含以下代码:

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

您的测试有:

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

据我从您的代码可以看出,您正在正确地模拟sessionFactory及其返回的会话.但是,您没有正确模拟出session.createCriteria(Employee.class,“ employees”)(相反,您正在设置session.createCriteria(ConceptModelDetails.class)的返回值).因此,模拟的返回值将为null.因此,当您立即取消引用(调用setResultTransformer)时,将获得NullPointerException.

要验证这一点,请尝试进入调试器,或将日志语句添加到EmployeeDAOImpl中,以显示sessionFactory,session的值以及session.createCriteria(…)的结果.

上一篇:java-如何在Mockito中模拟Spring依赖项


下一篇:java-使用Mockito模拟内部方法