Details
-
Bug
-
Resolution: Obsolete
-
Major
-
None
-
6.0.0.CR1
-
None
Description
Usage of Mockito spy actually creates a new instance (with current state) of the provided delegate. This can cause issues if there are already threads created that modify the provided delegate state as it won't affect the new object.
It would be safer to first use a forwarding mock instead that doesn't create another instance as this has caused issues with some tests regarding state transfer.
Here is an example of how the forwarding mock can be used.
StateConsumer sc = TestingUtil.extractComponent(cache, StateConsumer.class); final Answer<Object> forwardedAnswer = AdditionalAnswers.delegatesTo(sc); StateConsumer mockConsumer = mock(StateConsumer.class, withSettings().defaultAnswer(forwardedAnswer)); TestingUtil.replaceComponent(cache, StateConsumer.class, mockConsumer, true); doAnswer(new Answer() { @Override public Object answer(InvocationOnMock invocation) throws Throwable { // Do something return forwardedAnswer.answer(invocation); } }).when(mockConsumer).applyState(any(Address.class), anyInt(), anyCollection());