写单测的时候发现遇到了这个异常:
java.lang.IllegalStateException: 1 matchers expected, 3 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
但是本尊的测试是这么写的:
@Test
public void testExecuteSQL() throws Exception {
expect(queryInfoDao.saveQueryInfo(anyObject(QueryInfo.class))).andReturn(this.getQueryInfo());
executeQueryDaemon.addQueryInfo(anyObject(QueryInfo.class));
expectLastCall().once();
replayAll();
QueryInfo info = sqlExecuteService.executeSQL("abc", SessionInfo.ENGINETYPE_SPARK, "2f");
assertNotNull(info);
assertEquals("abc", info.getQuerySql());
assertEquals(SessionInfo.ENGINETYPE_SPARK, info.getEngineType());
assertEquals("2f", info.getUserName());
assertEquals(CommonUtil.getLocalHostName(), info.getTriggerHost());
}
但是它报错的位置是第三行,这简直他娘的冤枉啊,这里的确只注册了一个matcher啊。
网上各种查,实在查不到了,幸好哥这里有它的源码,那就直接在eclipse里debug源码吧,发现它的org.easymock.internal.LastCall里注册matcher的调用有一个奇怪的来源:
@Test
public void testIsQueryFinished() throws Exception {
QueryInfo fetchingInfo = this.getQueryInfo();
fetchingInfo.setStatus(QueryInfo.STATUS_FETCHING);
QueryInfo succeedInfo = this.getQueryInfo();
succeedInfo.setStatus(QueryInfo.STATUS_SUCCEED);
expect(queryInfoDao.getQueryInfoById(anyInt())).andReturn(fetchingInfo).andReturn(succeedInfo);
replayAll();
boolean result = sqlExecuteService.isQueryFinished(anyInt()); // QueryState 状态为EXECUTING,返回false
assertTrue(!result);
result = sqlExecuteService.isQueryFinished(anyInt()); // QueryState 状态为SUCCESS,返回true
assertTrue(result);
}
如上的第十行。
好吧,本尊他娘的突然明白了,Reply阶段是不可以用matcher的。将第10行和12行的anyInt()分别改为1和2就不会影响后面的测试了。