WrongTypeOfReturnValue exception on running test

1,786 views
Skip to first unread message

Danny

unread,
Nov 23, 2010, 11:42:14 AM11/23/10
to PowerMock
Hello,

I am stuck at an exception while running PowerMock test with TestNG. I
can't seem to figure out the reason why I am getting the exception. I
am using -

powermock-mockito-1.4.6-full.jar
mockito-all-1.8.5.jar
testng-5.14.jar.

Here's my test class -

@PrepareForTest(IESchedulerServiceBean.class)
public class SchedulerServiceBeanTest1
{

@Test
public void testFindAppointmentSearch() throws Exception {

IESchedulerServiceBean schedulerBean = spy(new
IESchedulerServiceBean());
ApptCalendarServiceBO mockApptCalendarBO = spy(new
ApptCalendarServiceBO());

//Mock private method call
PowerMockito.doReturn(mockApptCalendarBO).when(schedulerBean,
"getApptCalendarServiceBO");

AppointmentSearchVO appointmentSearchVO = spy(new
AppointmentSearchVO());
PowerMockito.when(schedulerBean,
"findAppointmentSearch",appointmentSearchVO).thenCallRealMethod();

ApptSearchResultInfoVO[] results =
schedulerBean.findAppointmentSearch(appointmentSearchVO );
Assert.assertNotNull(results);
}
}

On running the test I get the following error -

FAILED: testFindAppointmentSearch
org.mockito.exceptions.misusing.WrongTypeOfReturnValue:
ApptCalendarServiceBO$$EnhancerByMockitoWithCGLIB$$ad753252 cannot be
returned by findAppointmentSearch()
findAppointmentSearch() should return ApptSearchResultInfoVO[]
at org.powermock.api.mockito.PowerMockito.when(PowerMockito.java:421)
at
SchedulerServiceBeanTest1.testFindAppointmentSearch(SchedulerServiceBeanTest1.java:
77)
... Removed 30 stack frames

I have to use spy objects because I am working with legacy code that I
am not allowed to modify. It is worth noting that the real method
schedulerBean.findAppointmentSearch(appointmentSearchVO ) never gets
called. Seems like the exception is thrown because for some reason
instead of real method, mock method is being called. Any ideas why?
Any help will be appreciated.

Thanks.
Danny

Johan Haleby

unread,
Nov 23, 2010, 12:59:25 PM11/23/10
to powe...@googlegroups.com
Hi,

I haven't seen this error before. Unfortunately it's hard to tell what
going on unless you have a more complete example. Could you provide the
classes that you try to mock/test so that I can try to reproduce it? A
small example project demonstrating the issue would be even better.

/Johan

jchrisder

unread,
Nov 24, 2010, 6:58:23 AM11/24/10
to PowerMock
Hi, I am not sure that it is the same problem, but I encountered this
kind or error when I shared between different tests a class
initialized in a setUp junit method. It was a bit raw to do so, but
this does not occurred anymore since I initialize all my objects in
each test.

In fact this is perhaps related to Mockito and how it manages
concurrent mocking and not to PowerMock, since I found out this
comment in Mockito FAQ when I bumped into this error:

/*********************/
Is Mockito thread-safe?

For healthy scenarios Mockito plays nicely with threads. For instance,
you can run tests in parallel to speed up the build. Also, You can let
multiple threads call methods on a shared mock to test in concurrent
conditions. Check out a timeout() feature for testing concurrency.

However Mockito is only thread-safe in healthy tests, that is tests
without multiple threads stubbing/verifying a shared mock. Stubbing or
verification of a shared mock from different threads is NOT the proper
way of testing because it will always lead to intermittent behavior.
In general mutable state + assertions in multi-threaded environment
lead to random results. If you do stub/verify a shared mock across
threads you will face occasional exceptions like:
WrongTypeOfReturnValue, etc.
/*********************/

So perhaps there is a mock you are sharing in your test to do whatever
in different tests? Not sure it helps, but maybe you can follow this
path

See Mockito FAQ there: http://code.google.com/p/mockito/wiki/FAQ

JC

Danny

unread,
Dec 1, 2010, 11:45:45 AM12/1/10
to PowerMock
Sorry for my delayed response. I got pulled on another task and could
not investigate this problem further. This is not a multi-threading
problem as JC mentions because this is the only test I have. There
are no other tests. What seems to be happening is that my mocking of
the private method (getApptCalendarServiceBO of schedulerBean) seems
to be preventing the real method call (findAppointmentSearch of
schedulerBean). The private method is supposed to be called from the
findAppointmentSearch method but this real method never gets called.
Instead when I call this method in the test, it returns the result of
the mocked private method.

What we need to figure out is why is calling findAppointmentSearch
method is returning the result of mocked private method. Not sure how
much it will help but I here's the relevant code from schedulerBean
object anyway.


public ApptSearchResultInfoVO[]
findAppointmentSearch(AppointmentSearchVO appointmentSearchVO) {
VOAndDomainObjectConversion conversion=new
VOAndDomainObjectConversion();

IEScheduleAppt
tempieScheduleAppt=conversion.getIESchedApptFromAppointmentSearchVO(appointmentSearchVO);


ieScheduleApptList=getApptCalendarServiceBO().findAppointmentSearch(tempieScheduleAppt);


returnApptSearchResultInfoVOAry=conversion.getApptSearchResultInfoVO(ieScheduleApptList,
this.dailyDetailServiceBO,this.apptCalendarServiceBO);


return returnApptSearchResultInfoVOAry;
}

private IApptCalendarServiceBO getApptCalendarServiceBO() {
return apptCalendarServiceBO;
}


Thanks.
Danny
Reply all
Reply to author
Forward
0 new messages