EasyMock 常見異常


1.

java.lang.IllegalStateException: calling verify is not allowed in record state

含義:不允許在記錄狀態(record state)調用verify方法。

發生場景:不小心在調用EasyMock.replay(mockObj)之前,調用了EasyMock.verify(mockObj);

 

2.

java.lang.AssertionError:
Unexpected method call IDao.search(com.unittest.easymock.iargumentmatchertest.TestEntity@578486a3):
IDao.search(com.unittest.easymock.iargumentmatchertest.TestEntity@6ebc05a6): expected: 1, actual: 0

含義:從異常來看,search方法的參數的期待值和實際值不是同一個對象。

發生場景:在比較方法的入參的時候,一般是使用方法的equals方法來比較對象,如果equals方法沒有重寫而只是比較是否是同一個對象引用的話,很容易出現這種問題。

例:

    public void testEntity() {
        ServiceClass serviceClass = new ServiceClass();
        IDao idao = EasyMock.createMock(IDao.class);
        serviceClass.setIdao(idao);

        TestEntity testEntity = new TestEntity();
        TestEntity testEntity2 = new TestEntity();
        EasyMock.expect(idao.search(testEntity)).andReturn(2);

        EasyMock.replay(idao);

        serviceClass.search(testEntity2);

        EasyMock.verify(idao);
    }

解決方法:

1.重寫equals方法

2.使用EasyMock的Argument Matchers提供的或自定義的方法來比較方法入參。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM