Uploaded image for project: 'Infinispan'
  1. Infinispan
  2. ISPN-3671

Evaluate replacement of Mockito usage of spy with Forwarding mocks instead

    XMLWordPrintable

Details

    • Bug
    • Status: Closed (View Workflow)
    • Major
    • Resolution: Out of Date
    • 6.0.0.CR1
    • None
    • Test Suite
    • 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());
      

      Attachments

        Activity

          People

            wburns@redhat.com Will Burns
            wburns@redhat.com Will Burns
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: