Mockito:验证模拟(使用"RETURNS_DEEP_STUBS")返回比预期更多的调用

Mik*_*der 17 java mockito

看看下面的代码,我只希望调用getSand()发生一次,但是测试失败,只有四次调用.这些电话在哪里发生?我想写一个测试来确保只进行一次调用getSand().

资源

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();
    }
}
Run Code Online (Sandbox Code Playgroud)

产量

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)
Run Code Online (Sandbox Code Playgroud)

详细信息 Java 1.6,JUnit 4.11,Mockito 1.9.5

得到教训

如果您将深存根视为模拟对象树,那么您应该只验证叶子("最后模拟链"),因为节点包含在设置叶子行为所需的调用链中.为了这句话的另一种方式,节点叶子的安装过程中调用.

cru*_*dog 12

它将您的设置计为调用,因为验证API不支持深层存根,并在第二次调用时抱怨:

when(mockSandBox.getSand().doB()).thenReturn(1);
Run Code Online (Sandbox Code Playgroud)

我会跳过使用RETURNS_DEEP_STUBS并使用另一个模拟:

...
@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);
...
Run Code Online (Sandbox Code Playgroud)


hag*_*ard 10

来自Answers.RETURNS_DEEP_STUBS的文档:

Please see the {@link org.mockito.Mockito#RETURNS_DEEP_STUBS} documentation for more details.
Run Code Online (Sandbox Code Playgroud)

来自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();
Run Code Online (Sandbox Code Playgroud)

因此,我认为,为了让您的验证工作,您必须将您的模拟重写为以下内容:

@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();
}
Run Code Online (Sandbox Code Playgroud)

或者只验证doA,doB和doC的调用,而不验证getSand()的调用. - 这取决于你想在这里测试什么.