简体   繁体   English

为什么mockito在存根时调用父方法

[英]Why mockito make a call for parent method when stubbing

Why mockito make a call for stubbed method.为什么 mockito 调用存根方法。

Why it make an actual call for func under when..thenReturn I have checked when doing debugging.为什么它在 when..thenReturn 下实际调用func我在调试时检查过。

  @Test
    public void function(){
       MyClassChild obj = mock(MyClassChild.class);
       when(obj.func("abc")).thenReturn(3);
    }
    ...
    class MyClass {
       public int func(String s) {
          if (s.equals("abc"))
             return 3;
          else
             return 1;
       }
    }

class MyClassChild extends MyCLass {

    }

I attempted to replay your issue by having the following test:我尝试通过以下测试重播您的问题:

public class StackTest {
    @Test
    public void mockedFunction() {
        MyClass obj = mock(MyClass.class);
        when(obj.func("abc")).thenReturn(3);

        assertEquals(3, obj.func("abc"));
    }

    @Test
    public void function() {
        MyClass obj = new MyClass();

        assertEquals(7, obj.func("abc"));
    }
}

and

public class MyClass {

    public int func(String s) {
        if (s.equals("abc"))
            return 7;
        else
            return 9;
    }
}

All tests where executed successfully.成功执行的所有测试。 Can you show how you are invoking the test?你能展示你是如何调用测试的吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM