简体   繁体   中英

How to mock Instance DAO methods using PowerMock+EasyMock+Junit+Java

I am new to Unit Testing, got some requirement to mock DAO layer but not getting how to do it. Below is my sample DAO code which is doing some transactional activities.

 Class DAOList{
 public Object getRows(Map mapObj, Object error) {
    Session session = HibernateSessionManager.getSessionFactory().openSession();
    try {
        Query query = session.createSQLQuery("select * from some_table ").addEntity(SomeObject.class);
        Object getObj = query.uniqueResult();
        return getObj;
    }

    catch (HibernateException exc) {
        exc.printStackTrace();
        logger.error("Exception occurred: " + exc.getMessage());
    }

    finally {
        session.close();
    }
    return null;
}   }

I tried writing test case like below but I am getting Runtime exception

    @TestSubject 
    Session sessionn;

    @Mock 
    Transaction transaction;

    @MockSession 
    session;

    @MockSQLQuery
    mockQuery;

    @Test
public void testSomeSuccessCheck() throws Exception {
    HashMap map = new HashMap();

    EasyMock.expect(HibernateSessionManager.getSessionFactory().openSession()).andReturn(sessionn);
    EasyMock.expect(session.beginTransaction()).andReturn(transactionn);
    EasyMock.expect(session.createSQLQuery(EasyMock.anyString()).addEntity(VehicleDetails.class)).andReturn(query);
    EasyMock.expect((SomeObject) query.uniqueResult()).andReturn(someObj);

    SomeObject respObj = vehDao.someMethod(map, errs);
    assertNotNull(respObj);
}

Complete Error Trace which I got:

java.lang.RuntimeException: Invoking the beforeTestMethod method on PowerMock test listener org.powermock.api.extension.listener.AnnotationEnabler@5c87bfe2 failed.

Caused by: java.lang.NullPointerException: Have you forgotten to instantiate connection?
at org.easymock.internal.Injector.injectMocks(Injector.java:81)
at org.easymock.EasyMockSupport.injectMocks(EasyMockSupport.java:528)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.powermock.reflect.internal.WhiteboxImpl.performMethodInvocation(WhiteboxImpl.java:1846)
at org.powermock.reflect.internal.WhiteboxImpl.doInvokeMethod(WhiteboxImpl.java:810)
at org.powermock.reflect.internal.WhiteboxImpl.invokeMethod(WhiteboxImpl.java:790)
at org.powermock.reflect.Whitebox.invokeMethod(Whitebox.java:466)
at org.powermock.api.extension.listener.AnnotationEnabler.beforeTestMethod(AnnotationEnabler.java:73)
at org.powermock.tests.utils.impl.PowerMockTestNotifierImpl.notifyBeforeTestMethod(PowerMockTestNotifierImpl.java:82)

First you will need to mock the static method call HibernateSessionManager.getSessionFactory() and then mock the openSession () method call. Rest will remaining as what you have added.

Refer this for more information on how to use PowerMock : https://blog.codecentric.de/en/2016/03/junit-testing-using-mockito-powermock/

You can also use Mockito + Powermock to solve the same problem.

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