简体   繁体   English

如何使用 PowerMock+EasyMock+Junit+Java 模拟实例 DAO 方法

[英]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.我是单元测试的新手,对模拟 DAO 层有一些要求,但不知道如何去做。 Below is my sample DAO code which is doing some transactional activities.下面是我的示例 DAO 代码,它正在执行一些事务性活动。

 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. java.lang.RuntimeException:在 PowerMock 测试侦听器 org.powermock.api.extension.listener.AnnotationEnabler@5c87bfe2 上调用 beforeTestMethod 方法失​​败。

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.首先,您需要模拟静态方法调用HibernateSessionManager.getSessionFactory() ,然后模拟openSession () 方法调用。 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/有关如何使用 PowerMock 的更多信息,请参阅此处: https ://blog.codecentric.de/en/2016/03/junit-testing-using-mockito-powermock/

You can also use Mockito + Powermock to solve the same problem.你也可以使用 Mockito + Powermock 来解决同样的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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