【问题标题】:EasyMock throwing matcher error on method call recording with no matchers?EasyMock 在没有匹配器的方法调用记录中抛出匹配器错误?
【发布时间】:2016-02-27 01:10:01
【问题描述】:

我正在使用 EasyMock 3.4 并且有一个单元测试我收到一个非常奇怪的错误。

堆栈跟踪是:

java.lang.IllegalStateException: 0 matchers expected, 1 recorded.
This exception usually occurs when matchers are mixed with raw values when recording a method:
    foo(5, eq(6));  // wrong
You need to use no matcher at all or a matcher for every single param:
    foo(eq(5), eq(6));  // right
    foo(5, 6);  // also right
    at org.easymock.internal.ExpectedInvocation.createMissingMatchers(ExpectedInvocation.java:51)

我的单元测试如下:

    @Test
    public void testAddOrUpdateGuestMessageScenario(){
        final Session mockSession = EasyMock.createMock(Session.class);
        final Query mockQuery = EasyMock.createMock(Query.class);
        final Transaction mockTransaction = EasyMock.createMock(Transaction.class);
        final GuestMessageScenarioBO guestMessageScenarioBOMock = EasyMock.createMock(GuestMessageScenarioBO.class);
        final MessageCampaignBO messageCampaignBOMock = EasyMock.createMock(MessageCampaignBO.class);
        final InboundMessageSimulatorBO inboundMessageSimulatorBOMock = EasyMock.createMock(InboundMessageSimulatorBO.class);

        EasyMock.expect(guestMessageScenarioBOMock.getInboundMessageSimulatorBO()).andReturn(inboundMessageSimulatorBOMock).anyTimes();
        EasyMock.expect(inboundMessageSimulatorBOMock.getMessageCampaignBO()).andReturn(messageCampaignBOMock);
        EasyMock.expect(inboundMessageSimulatorBOMock.getCreateDate()).andReturn(null);
        EasyMock.expect(messageCampaignBOMock.getMessageCampaignRecordId()).andReturn(1234L);
        EasyMock.expect(mockSession.get(MessageCampaignBO.class, 1234L)).andReturn(messageCampaignBOMock);

        guestMessageScenarioBOMock.setCreateDate(EasyMock.isA(Timestamp.class));
        guestMessageScenarioBOMock.setUpdateDate(EasyMock.isA(Timestamp.class));
        inboundMessageSimulatorBOMock.setMessageCampaignBO(messageCampaignBOMock);
        inboundMessageSimulatorBOMock.setCreateDate(EasyMock.isA(Timestamp.class));
        inboundMessageSimulatorBOMock.setUpdateDate(EasyMock.isA(Timestamp.class));
        inboundMessageSimulatorBOMock.setGuestBusinessRuleUsageIndicator(true);

        EasyMock.expect(mockSession.createQuery(EasyMock.isA(String.class))).andReturn(mockQuery);      
        EasyMock.expect(mockSessionFactory.openSession()).andReturn(mockSession).anyTimes();
        mockSession.close();
        EasyMock.expectLastCall();
        EasyMock.expect(mockSession.beginTransaction()).andReturn(mockTransaction);
        mockSession.saveOrUpdate(EasyMock.isA(GuestMessageScenarioBO.class));
        mockTransaction.commit();
        EasyMock.replay(mockSessionFactory, mockSession, mockQuery,mockTransaction, guestMessageScenarioBOMock, inboundMessageSimulatorBOMock, messageCampaignBOMock);
        guestMessageScenarioDAO.addOrUpdateGuestMessageScenario(guestMessageScenarioBOMock);
    }

它抱怨的那一行是:

EasyMock.expect(guestMessageScenarioBOMock.getInboundMessageSimulatorBO()).andReturn(inboundMessageSimulatorBOMock).anyTimes();

我知道在 EasyMock 中,您不能在方法调用中将匹配器与实际值/对象混合,否则会引发错误,但很明显,查看该代码时根本没有使用匹配器,更不用说与任何东西混合因为 guestMessageScenarioBOMock 上的那个方法甚至不带任何参数。

知道它在抱怨什么吗?

更新:包括@Before 方法

@Before
public void onSetup() {
    mockSessionFactory = EasyMock.createMock(SessionFactory.class);
    guestMessageScenarioDAO = new GuestMessageScenarioDAO();
    guestMessageScenarioDAO.setSessionFactory(mockSessionFactory);
}

【问题讨论】:

  • 继承是否有可能导致这种情况?也许 EasyMock 为子类和父类(或接口)创建了一个匹配器,结果是不同的对象类型?
  • @kondrak 你能详细说明一下吗? guestMessageScenarioBOMock 确实继承自另一个类,但我不确定要查找什么会导致此问题。
  • 我的意思是你可能必须小心你在嘲笑什么,因为可能会发生这样的事情:pastebin.com/HKB4iaNX
  • 不,guestMessageScenarioBOMock 只是简单地扩展了另一个类,它看起来不像在做那样的事情。还有其他想法吗?这真的让我发疯了......
  • 只是出于好奇,当您从它抱怨的行尾删除“.anyTimes()”时会发生什么?

标签: java unit-testing junit mocking easymock


【解决方案1】:

问题与在之前的测试中,模拟对象记录的方法调用没有跟随期望有关。

当这种情况发生时,EasyMock 尝试将预期与记录的方法调用相匹配,即使在另一个测试中也是如此。

mockedObj.doSomething();

EasyMock.expectLastCall(); //This was missing and is needed.

希望这对其他人有所帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-08
    • 2012-10-06
    • 1970-01-01
    相关资源
    最近更新 更多