Mockito在模拟方法的参数上调用方法 [英] Mockito call a method on a parameter of a mocked method

查看:133
本文介绍了Mockito在模拟方法的参数上调用方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚开始使用Mockito,我只想做类似的事情:

I am just starting with Mockito and I just want to do something like :

public class Test {     
    public void clearList(List l){
        doVeryLOOOONGDatabaseCallll();
        l.clear();
        return;
    }
}

/// ... 
Test test = mock(Test.class);
Mockito.when(test.clearList(any(List.class))).then( l => l.clear());

有一些暗示可以做到这一点吗?
感谢您的帮助!

Have some hint to do the trick? Thank you for your help!

推荐答案

这样的事情应该这样做(未经测试):

Something like this should do it (not tested):

doAnswer(new Answer() {
    public Object answer(InvocationOnMock invocation) {
        Object[] args = invocation.getArguments();
        List<?> list = (List<?>) args[0];
        list.clear();
        return null;
    }
}).when(test).clearList(any(List.class));

这篇关于Mockito在模拟方法的参数上调用方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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