我使用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);
}
答案 0 :(得分:0)
这个问题与以下事实有关:在之前的测试中,模拟对象记录了一个没有预期的方法调用。
当发生这种情况时,即使在另一个测试中,EasyMock也会尝试将预期与录制的方法调用相匹配。
mockedObj.doSomething();
EasyMock.expectLastCall(); //This was missing and is needed.
希望这有助于其他人。