Mockito:验证Mockito(带有"RETURNS_DEEP_STUBS")返回的呼叫数量超出预期 [英] Mockito: Verify Mock (with "RETURNS_DEEP_STUBS") Returns More Calls Than Expected

查看:946
本文介绍了Mockito:验证Mockito(带有"RETURNS_DEEP_STUBS")返回的呼叫数量超出预期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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().

来源

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();
    }
}

输出

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)

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

Details Java 1.6, JUnit 4.11, Mockito 1.9.5

如果您将深层存根视为模拟对象的树,则应仅验证叶子(链中的最后一个模拟"),因为节点已包含在设置叶子行为所需的调用链中.换句话说,在叶子设置期间调用了节点 .

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.

推荐答案

由于Deep存根为

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);

我会跳过使用RETURNS_DEEP_STUBS,而仅使用另一个模拟:

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);
...

这篇关于Mockito:验证Mockito(带有"RETURNS_DEEP_STUBS")返回的呼叫数量超出预期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆