简体   繁体   中英

Unit test of DAO layer with mockito

I am trying to use Mockito for unit testing of my Spring + Hibernate project. Following is the implementation of my DAO class:

@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();
    }

}

Following is my test class:

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 is the dummy array list of Employees. Please suggest what is he issue here.When I run this test case, I am getting a failure with Null Pointer Exception in :

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

Stack trace:

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 contains the following code:

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

Your test has:

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

You are correctly mocking out sessionFactory and the session that it is returning, as far as I can tell from your code. However, you are not correctly mocking out session.createCriteria(Employee.class, "employees") (instead, you are setting the return value for session.createCriteria(ConceptModelDetails.class) ). Therefore, the return value from the mock will be null . So when you dereference it immediately (calling setResultTransformer ), you will get a NullPointerException .

To verify this, try stepping into the debugger, or adding log statements to EmployeeDAOImpl , that show the values of sessionFactory , session , and the result from session.createCriteria(...) .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM