EasyMock异常“java.lang.IllegalStateException: 1 matchers expected, 3 recorded”的奇葩原因

写单测的时候发现遇到了这个异常:


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就不会影响后面的测试了。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值