简体   繁体   English

Mockito 间谍不是 mocking 对模拟方法的内部调用

[英]Mockito spy not mocking internal call to mocked method

I have a class that calls out to a third party system to get data that I want to mock using Mockito. Here is some pseudo-code of how my class is setup.我有一个 class,它调用第三方系统来获取我想使用 Mockito 模拟的数据。这是我的 class 设置方式的一些伪代码。

// class I will be spying
public class SomeService {
    // method I want to full test
    public List<String> methodA(List<String> list) {
        ... omitted code ...
        // internal call to method in the same class that will be mocked
        innerList = methodB(list);
        ... omitted code ...
    }

    // method that I want to mock/spy
    public List<String> methodB(List<String> list) {
        //details omitted since it should be mocked any never called
    }
}

Here is my test code.这是我的测试代码。 I setup my spy in a BeforeEach method to make sure that Mock is configured.我在 BeforeEach 方法中设置我的间谍以确保 Mock 已配置。 When I directly call the mocked method, everything works as expected.当我直接调用模拟方法时,一切都按预期进行。 When I call当我打电话

public class SomeServiceTest{

    private SomeService service;

    @BeforeEach
    public void init() {
        service = Mockito.spy(SomeService.class);
        // also tried with creating a new object but has same failure conditions
        //service = Mockito.spy(new SomeService());

        // mocking methodB with answer details omitted to reduce clutter
        Mockito.doAnswer(..detail omitted..).when(service).methodB(Mockito.anyList());
    }

    //testing directly calling mocked method that returns mocked data
    @Test
    public void testMockedMethod() throws Exception {
        List<String> list = //setup details ignored
        List<String> results = service.methodB(list);
        // follow up asserts work
    }

    //testing indirectly calling mocked method that call actual method code
    @Test
    public void testMethodA() throws Exception {
        List<String> list = //setup details ignored
        // NullPointer in methodB since mocking is ignore
        List<String> results = service.methodA(list);
        // assertions never tested since NPE was thrown
    }
}

Does anyone know why when I directly call the mocked method, it returns the doAnswer;有谁知道为什么当我直接调用模拟方法时,它返回 doAnswer; but when I indirectly call from within the mocked/spied object it ignores the fact that the method is mocked and calls the original method?但是当我从模拟/间谍 object 中间接调用时,它忽略了该方法被模拟并调用原始方法的事实?

It should work.它应该工作。 It seems to me that the NPE exception is because there are some bugs in your codes which already happen before it really executes the stubbed method.在我看来,NPE 异常是因为您的代码中存在一些错误,这些错误在它真正执行存根方法之前就已经发生了。 What is the stacktrack that you get?你得到的堆栈跟踪是什么?

On the other hand, you can also add some println just before and after calling the stubbed method to verify the codes is really can execute up to that point:另一方面,您还可以在调用存根方法之前和之后添加一些println来验证代码是否真的可以执行到那一点:

 public List<String> methodA(List<String> list) {
    .......
    System.out.println("before methodB");
    innerList = methodB(list);
    System.out.println("after methodB which return " + innerList);
    ....
}

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

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