从新对象调用方法时,是否可以通过捕获(例如捕获)返回模拟?
为了更具体:
SecurityInterface client = new SecurityInterface();
port = client.getSecurityPortType(); --> I want to mock this.
easymock版本:3.3.1
最佳答案
是的,如果您还使用Powermock,则您的测试代码可以拦截对new
的调用并返回模拟。因此,您可以为new SecurityInterface()
返回一个模拟,然后模拟其getter
Powermock与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);
}
}
关于java - EasyMock-从新对象返回的模拟对象,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/33241635/