java - EasyMock - 从新对象返回的模拟对象

标签 java junit easymock

例如,当从新对象调用方法时,是否可以通过捕获返回模拟?

为了使其更具体:

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/

相关文章:

java - 是否可以要求 junit 在进行测试之前运行处方?

java - 如何在 Eclipse 中的 git 分支之间进行纯文本比较(使用 EGit)

java - 在 Amazon Elasticbeanstalk 中运行 netty 应用程序

java - Junit 测试用例 - 错误 - 预期状态 :<200> but was:<500>

java - Mockito.anyLong() 是做什么的?

java - Powermock 未返回正确的对象静态方法

Java编译时错误: Compiler not recognizing method override

java - JUnit - 模拟服务嵌套映射器

java - 使用 EasyMock 测试参数值

java - 类数据类型的 EasyMock 匹配器