【问题标题】:EasyMock expected voidEasyMock 预期无效
【发布时间】:2013-04-30 19:46:19
【问题描述】:

第一次尝试 EasyMock。

我似乎明白了,但我立即停止了模拟类运行的方法“返回”void (EntityManager.remove(abc))。

我可以部分模拟 EntityManger 以开始测试,即

EasyMock.expect(this.mockManager.find(Some.class, id)).andReturn(mock);

,但是对于“删除”案例,我该如何做呢?

我做不到(例如):

EasyMock.expect(this.mockManager.remove(rek)).andReturn(Boolean(true));

如果我什么都不做,我会得到:

java.lang.AssertionError: 
Unexpected method call EntityManager.remove(EasyMock for class my.package.Some)...

我需要在删除部分之前测试逻辑,但我不在乎它是否真的成功(会是另一回事)。

【问题讨论】:

    标签: jpa-2.0 easymock


    【解决方案1】:

    您无需致电EasyMock.expect()。只需使用

    this.mockManager.remove(rek);
    

    在录制阶段(在调用replay()之前)。

    如果您希望模拟方法,例如,抛出异常或被调用两次,请使用expectLastCal()

    this.mockManager.remove(rek);
    expectLastCall().andThrow(new RuntimeException());
    //or expectLastCall().times(2);
    

    【讨论】:

    • 在我再次检查这里之前,我实际上只是自己弄清楚了这一点(尽管还没有异常部分)。你速度很快。 +1 为此。
    猜你喜欢
    • 1970-01-01
    • 2010-10-25
    • 2011-06-08
    • 1970-01-01
    • 2013-12-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-10
    相关资源
    最近更新 更多