when I run mockito test occurs WrongTypeOfReturnValue Exception

confused windbell picture confused windbell · Jun 20, 2012 · Viewed 113.3k times · Source

Error detail:

org.mockito.exceptions.misusing.WrongTypeOfReturnValue: 
Boolean cannot be returned by updateItemAttributesByJuId()
updateItemAttributesByJuId() should return ResultRich
This exception might occur in wrongly written multi-threaded tests.
Please refer to Mockito FAQ on limitations of concurrency testing.

my code :

@InjectMocks
protected ItemArrangeManager arrangeManagerSpy = spy(new ItemArrangeManagerImpl());
@Mock
protected JuItemWriteService juItemWriteService;

when(arrangeManagerSpy
    .updateItemAttributes(mapCaptor.capture(), eq(juId), eq(itemTO.getSellerId())))
    .thenReturn(false);

As you can see, I am calling when on updateItemAttributes (which does return a boolean) not on updateItemAttributesByJuId.

  1. Why is Mockito attempting to return a boolean from updateItemAttributesByJuId?
  2. How can this be rectified?

Answer

gna picture gna · Jul 17, 2012

According to https://groups.google.com/forum/?fromgroups#!topic/mockito/9WUvkhZUy90, you should rephrase your

when(bar.getFoo()).thenReturn(fooBar)

to

doReturn(fooBar).when(bar).getFoo()