【问题标题】:PowerMock + EasyMock unexpected method callPowerMock + EasyMock 意外方法调用
【发布时间】:2013-10-17 21:53:24
【问题描述】:

我有一个这样的方法,我想对它进行单元测试

public void update(String collectionName, BasicDBObject query, BasicDBObject updateObj){
    try{
        DBCollection collection = getCollection(collectionName);
        collection.update(query, updateObj, true, false);

    } catch (MongoException e) {
        if (e.getMessage().startsWith("can't call something")) {
               refreshConnection(collectionName);
            } else {
                throw e;
            }
    }
}

测试代码如下。我已经在单元测试用例的 cmets 中尝试了这两种方法,目前已经对其进行了评论。

@Test
public void testUpdate(){
    MongoStore store = PowerMock.createStrictPartialMockForAllMethodsExcept(MongoStore.class, "update");
    DBCollection collection = PowerMock.createMock(DBCollection.class);

    BasicDBObject updateobj = new BasicDBObject("test","shrikar");
    String name = "testcoll";
    String id = "123";
    BasicDBObject query = new BasicDBObject("id",id);
    EasyMock.expect(store.getCollection(name)).andReturn(collection);
    //EasyMock.expect(collection.update(EasyMock.anyObject(BasicDBObject.class),EasyMock.anyObject(BasicDBObject.class),EasyMock.anyBoolean(),EasyMock.anyBoolean()));
    //EasyMock.expect(collection.update(query,updateobj,true,false));
    PowerMock.replayAll();
    store.update(name,query,updateobj);
    EasyMock.expectLastCall().times(1);

    PowerMock.verifyAll();
}

在所有情况下,我不断得到

Unexpected Method call DBCollection.update({"id":"123"},{"test":"shrikar"}, true, false)

我错过了什么?

【问题讨论】:

    标签: unit-testing junit4 easymock powermock


    【解决方案1】:

    好的,我发现了我忘记将 Mongo*.class 添加到 PrepareForTest 的问题

    @PrepareForTest({MongoStore.class,MongoOptions.class,Mongo.class, BasicDBObject.class,DBCursor.class,DBObject.class})
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-03-06
      • 2011-01-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-02-24
      • 1970-01-01
      相关资源
      最近更新 更多