EasyMock-从新对象返回的模拟对象 [英] EasyMock - mock object returned from new Object
本文介绍了EasyMock-从新对象返回的模拟对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
例如从捕获中,当从新对象中调用方法时是否可以返回模拟?
Is it possible, with a capture for example, to return a mock when a method is called from a new object?
更具体一点:
SecurityInterface client = new SecurityInterface();
port = client.getSecurityPortType(); --> I want to mock this.
easymock版本:3.3.1
easymock version: 3.3.1
推荐答案
是的,如果您还使用 Powermock 您的测试代码可以拦截对new
的调用并返回一个模拟.因此,您可以为new SecurityInterface()
返回一个模拟,然后模拟其getter
Yes, if you also use Powermock your test code can intercept the calls to new
and return a mock instead. So you can return a mock for new SecurityInterface()
and then mock its getter
Powermock与Easymock兼容
Powermock is compatible with Easymock
@RunWith(PowerMockRunner.class)
@PrepareForTest( MyClass.class )
public class TestMyClass {
@Test
public void foo() throws Exception {
SecurityInterface mock = createMock(SecurityInterface.class);
//intercepts call to new SecurityInterface and returns a mock instead
expectNew(SecurityInterface.class).andReturn(mock);
...
replay(mock, SecurityInterface.class);
...
verify(mock, SecurityInterface.class);
}
}
这篇关于EasyMock-从新对象返回的模拟对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文