Mockito: Verify Mock (with "RETURNS_DEEP_STUBS") Returns More Calls Than Expected
Asked Answered
H

3

24

Looking at the code below, I only expect the call to getSand() to happen once, but the test is failing with four calls to it. Where are these calls happening? I want to write a test to insure that only one call is made to getSand().

Source

import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Answers;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;

import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

@RunWith(MockitoJUnitRunner.class)
public class DeepSandTest {

    @Mock(answer = Answers.RETURNS_DEEP_STUBS)
    SandBox mockSandBox;

    @Test
    public void should(){
        when(mockSandBox.getSand().doA()).thenReturn(1);
        when(mockSandBox.getSand().doB()).thenReturn(1);
        when(mockSandBox.getSand().doC()).thenReturn(1);

        DeepSand deepSand = new DeepSand(mockSandBox);
        deepSand.getTipple();

        verify(mockSandBox, times(1)).getSand();
    }

    public class DeepSand{

        private SandBox sandBox;

        public DeepSand(SandBox sandBox) {
            this.sandBox = sandBox;
        }

        public void getTipple(){
            Sand sand = sandBox.getSand();
            sand.doA();
            sand.doB();
            sand.doC();
        }
    }

    public interface SandBox{
        public Sand getSand();
    }

    public interface Sand{
        public Integer doA();
        public Integer doB();
        public Integer doC();
    }
}

Output

org.mockito.exceptions.verification.TooManyActualInvocations: 
mockSandBox.getSand();
Wanted 1 time:
-> at DeepSandTest.should(DeepSandTest.java:26)
But was 4 times. Undesired invocation:
-> at DeepSandTest.should(DeepSandTest.java:20)

Details Java 1.6, JUnit 4.11, Mockito 1.9.5

Lessons Learned

If you think of deep stubs as a tree of mock objects, then you should only verify the leaves ("last mock in the chain") because the nodes are included in the call chain needed to setup the behavior of the leaves. To phrase this another way, the nodes are called during the setup of the leaves.

Here answered 13/11, 2013 at 20:25 Comment(0)
A
17

It's counting your setup as invocations since deeps stubs is not supported in the verification API, and complains on the second call which is:

when(mockSandBox.getSand().doB()).thenReturn(1);

I would skip using RETURNS_DEEP_STUBS and just use another mock:

...
@Mock
SandBox mockSandBox;

@Mock
Sand sand;

@Test
public void should(){
    when(mockSandBox.getSand()).thenReturn(sand);
    when(sand.doA()).thenReturn(1);
    when(sand.doB()).thenReturn(1);
    when(sand.doC()).thenReturn(1);
...
Authors answered 13/11, 2013 at 20:41 Comment(2)
That's weird, any idea why it is counting the setup?Here
I think it's because of what many others are saying already, the verification API don't support deep stubs, so it cannot differ from real invocation and stubbed invocation: docs.mockito.googlecode.com/hg/org/mockito/…Authors
B
13

From the documentation of Answers.RETURNS_DEEP_STUBS:

Please see the {@link org.mockito.Mockito#RETURNS_DEEP_STUBS} documentation for more details.

From Mockito.RETURNS_DEEP_STUBS:

Verification only works with the last mock in the chain. You can use verification modes. 
[...]
when(person.getAddress(anyString()).getStreet().getName()).thenReturn("deep");
[...]
inOrder.verify(person.getAddress("the docks").getStreet(), times(1)).getName();

So, I think, in order to get your verifies to work, you have to rewrite your Mocks to something like this:

@Mock
SandBox mockSandBox;

@Mock
Sand mockSand;

@Test
public void should()
{
    when( mockSand.doA() ).thenReturn( 1 );
    when( mockSand.doB() ).thenReturn( 1 );
    when( mockSand.doC() ).thenReturn( 1 );

    when( mockSandBox.getSand() ).thenReturn( mockSand );

    DeepSand deepSand = new DeepSand( mockSandBox );
    deepSand.getTipple();

    verify( mockSandBox, times( 1 ) ).getSand();
}

Or only verify the invocations of doA, doB and doC and not verify the invocation of getSand(). - Which depends on what exactly you want to test for here.

Bullivant answered 13/11, 2013 at 20:38 Comment(5)
That is how deep stubs work internally. (docs.mockito.googlecode.com/hg/latest/org/mockito/…). Based on crunchdog's answer I believe the question is now: Why is it counting the when statements as invocations?Here
Ah yes, but in your case your are actively calling getSand() on the first mock several times. So that i guess is it. And the doc states several times that verification only works on the last mock in the chain.Bullivant
in verify(mockSandBox, times(1)).getSand(); the mockSandbox is the last mock in the chain. I am not sure what you are implying.Here
I think what the documentation means with last mock in the chain, is the last thing you call during setup. E.g. the doA() calls : when(mockSandBox.getSand().doA()).thenReturn(1);Bullivant
Ok I think I get it now. If you think of deep stubs as a tree of mock objects, then you should only verify the leaves ("last mock in the chain") because the nodes have to be called to setup the behavior of the leaves.Here
D
0

From documention: " Verification API does not support 'chaining' so deep stub doesn't change how you do verification."

Source: http://mockito.googlecode.com/svn/tags/1.8.3/javadoc/org/mockito/Mockito.html#RETURNS_DEEP_STUBS

Delete answered 13/11, 2013 at 20:37 Comment(2)
I am using Mockito 1.9.5 which does not have that warning anymore. (docs.mockito.googlecode.com/hg/latest/org/mockito/…)Here
Verification only works with the last mock in the chain. Link to latest doc : javadoc.io/doc/org.mockito/mockito-core/latest/org/mockito/…Illeetvilaine

© 2022 - 2024 — McMap. All rights reserved.