Looks like you're attempting to mock the underlying driver or ORM code.
I once tried to mock the Hibernate calls I was using. This was a lost cause.
Typically, one doesn't mock classes one doesn't own. Instead, mock your
DAO layer itself for testing your business layer on top. There are
different techniques for "unit" testing the DAO/ORM/driver layer.
Others may have better advice and I'm on the look-out myself for a
simple way to test my DAO code, but I haven't found it, so I try to keep
my DAO code as thin as possible.
On 9/18/2012 8:47 AM, Tarun wrote:
> Hi,
> I am new to Mockito and trying to mock a dao class, but when it reach
> to the finally block where I am closing the ResultSet then it is
> giving me below error:
> org.mockito.exceptions.base.MockitoException:
> 'close' is a *void method* and it *cannot* be stubbed with a *return
> value*!
> Voids are usually stubbed with Throwables:
> doThrow(exception).when(mock).someVoidMethod();
> ***
> If you're unsure why you're getting above error read on.
> Due to the nature of the syntax above problem might occur because:
> 1. The method you are trying to stub is *overloaded*. Make sure you
> are calling the right overloaded version.
> 2. Somewhere in your test you are stubbing *final methods*. Sorry,
> Mockito does not verify/stub final methods.
> 3. A spy is stubbed using when(spy.foo()).then() syntax. It is safer
> to stub spies -
> - with doReturn|Throw() family of methods. More in javadocs for
> Mockito.spy() method.
>
> at
> com.impetus.neustar.portps.commons.service.TestOracleDAO.test(TestOracleDAO.java:69)
> 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:616)
> at
> org.junit.internal.runners.TestMethodRunner.executeMethodBody(TestMethodRunner.java:99)
> at
> org.junit.internal.runners.TestMethodRunner.runUnprotected(TestMethodRunner.java:81)
> at
> org.junit.internal.runners.BeforeAndAfterRunner.runProtected(BeforeAndAfterRunner.java:34)
> at
> org.junit.internal.runners.TestMethodRunner.runMethod(TestMethodRunner.java:75)
> at
> org.junit.internal.runners.TestMethodRunner.run(TestMethodRunner.java:45)
> at
> org.junit.internal.runners.TestClassMethodsRunner.invokeTestMethod(TestClassMethodsRunner.java:71)
> at
> org.junit.internal.runners.TestClassMethodsRunner.run(TestClassMethodsRunner.java:35)
> at
> org.junit.internal.runners.TestClassRunner$1.runUnprotected(TestClassRunner.java:42)
> at
> org.junit.internal.runners.BeforeAndAfterRunner.runProtected(BeforeAndAfterRunner.java:34)
> at
> org.junit.internal.runners.TestClassRunner.run(TestClassRunner.java:52)
> at
> org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:49)
> at
> org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
> at
> org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
> at
> org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
> at
> org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
> at
> org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
>
> And my test method is :
> @Test
> public void test() throws Exception{
> MockitoAnnotations.initMocks(this);
> systemUnderTest = new OracleDAOImpl();
> systemUnderTest.setDBConnectionManager(connectionManager);
> Set<NACustomerDTO> set = new HashSet<NACustomerDTO>();
> when(connectionManager.getDataSource()).thenReturn(dataSource);
> //.getConnection().createStatement().executeQuery("select
> PORTPS_CUSTOMER_ID, NPAC_SPID from PORTPS_CUSTOMER_SPID_ASSOC where
> PORTPS_CUSTOMER_ID IN (SELECT UNIQUE PORTPS_CUSTOMER_ID FROM
> PORTPS_CUSTOMER_SVCS_ASSOC WHERE SERVICE = 'NA_CUSTOMER') ORDER BY
> PORTPS_CUSTOMER_ID,NPAC_SPID")).thenReturn(resultSet);
> when(dataSource.getConnection()).thenReturn(connection);
> when(connection.createStatement()).thenReturn(statement);
> when(statement.executeQuery(anyString())).thenReturn(resultSet);
> when(resultSet.next()).thenReturn(false);
> when(resultSet.getLong(1)).thenReturn(1L);
> when(resultSet.getString(2)).thenReturn("7178");
>
> doNothing().when(resultSet).close();
>
> stub(systemUnderTest.getNACustomers()).toReturn(set);
> final Set<NACustomerDTO> result =
> systemUnderTest.getNACustomers();
>
> verify(connectionManager).getDataSource();
> verify(dataSource).getConnection();
> verify(connection).createStatement();
> verify(statement).executeQuery(anyString());
> verify(resultSet).next();
> verify(resultSet).getLong(1);
> verify(resultSet).getString(2);
>
> assertNotNull(result);
>
> //verify(connectionManager).getDataSource().getConnection();
> verify(connectionManager).getDataSource().getConnection();
> }
>
> Please help !
>
> --
> You received this message because you are subscribed to the Google
> Groups "mockito" group.
> To view this discussion on the web visit
>
https://groups.google.com/d/msg/mockito/-/8SPtSKfQXZoJ.
> To post to this group, send email to
moc...@googlegroups.com.
> To unsubscribe from this group, send email to
>
mockito+u...@googlegroups.com.
> For more options, visit this group at
>
http://groups.google.com/group/mockito?hl=en.