21

查看下面的代码,我只希望调用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();
    }
}

输出

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

得到教训

如果您将深度存根视为模拟对象的树,那么您应该只验证叶子(“链中的最后一个模拟”),因为节点包含在设置叶子行为所需的调用链中。换一种说法,在叶子的设置过程中调用节点。

4

3 回答 3

15

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

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

我会跳过使用 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);
...
于 2013-11-13T20:41:20.967 回答
13

从 Answers.RETURNS_DEEP_STUBS 的文档中:

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

来自 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();

所以,我认为,为了让您的验证工作,您必须将您的 Mocks 重写为如下内容:

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

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

于 2013-11-13T20:38:28.597 回答
0

来自文档:“验证 API 不支持‘链接’,因此深度存根不会改变您进行验证的方式。”

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

于 2013-11-13T20:37:11.493 回答