Mockito: Verify Mock (with “RETURNS_DEEP_STUBS”) Returns More Calls Than Expected

后端 未结 3 2112
广开言路
广开言路 2021-02-18 20:22

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 wan

3条回答
  •  死守一世寂寞
    2021-02-18 20:57

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

提交回复
热议问题