java - 检查 EasyMock 匹配器中的属性值

标签 java unit-testing mocking easymock

想象一下,我有一个 EasyMock 测试,其中有以下几行:

final IRunControl runControl = createMock(IRunControl.class);
runControl.setSomething(isA(ISomething.class));
EasyMock.expectLastCall().once();

ISomething 看起来像这样:

interface ISomething
{
    int getValue1();

    String getValue2();
}

是否可以让 runControl.setSomething(isA(ISomething.class)) 检查属性值?

我。 e.做类似的事情

runControl.setSomething(
    and(
        isA(ISomething.class),
        and(propertyValue("value1", 123), propertyValue("value2", "expectedValue2")))

最佳答案

你需要的是使用Capture .

一个例子:

    // setup: data
    ISomething fooSomething = ISomethingImpl(5, "bar"); 

    // setup: expectations
    Capture<ISomething> capturedISomething = new Capture<ISomething>();
    mockCollaborator.setSomething(capture(capturedISomething));

    // exercise
    replay(mockCollaborator);
    sut.dooWhateverThatInvokesTheCollaboratorSetter(fooSomething);

    // verify
    verify(mockCollaborator);
    assertEquals(5, capturedISomething.getValue().getValue1());
    assertEquals("bar", capturedISomething.getValue().getValue2());

关于java - 检查 EasyMock 匹配器中的属性值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26120956/

相关文章:

java - 如何在android中解析这种XML

java - 以编程方式计算值类型的大小

带 url 参数的 python 模拟响应

testing - 模拟和 stub 之间有什么区别?

c# - 那些单元测试没问题吗?

java - 如何刷新速度模板

java - 如何创建一个虚拟 REST 服务器 API 来检查我的 android http 请求\响应?

Java ActionListener 错误 : incompatible types

visual-studio - 运行 Visual Studio ASP.NET 单元测试时出现 500 错误

unit-testing - 在 MSTest/VS 单元测试中是否可以有确定性的 ClassCleanup?