-1

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)
Vishwas Sampath
  • 113
  • 1
  • 2
  • 8
  • 1
    have you tried google? i don't mean to come across rude. but. there's tons of hits out there with your name all over them. –  Feb 23 '18 at 14:14
  • I have tried it and getting Runtime exception.I have edited my question. Please have a look. – Vishwas Sampath Feb 23 '18 at 14:59

1 Answers1

0

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.

lrathod
  • 1,094
  • 1
  • 9
  • 17