I am doing unit test case for mongoDB read operation. When I try to mock find operation, I am getting a NullPointerException
public class MongoQueriertest {
HelperUtil testhelper = new HelperUtil();
DBCollection feedCollection;
DBCursor curr;
@Test(expected=NullPointerException.class)
public void test_MongoQuerier ()throws Exception {
MongoQuerier query = new MongoQuerier();
FindIterable iterable = mock(FindIterable.class);
curr = mock(DBCursor.class);
BasicDBObject obj = mock(BasicDBObject.class);
when(feedCollection.find(obj)).thenReturn((DBCursor) iterable);
query.getMaxId("String");
}
}
Method to test
public String getMaxId(String dataVersion) throws Exception{
String maxJobId = "1";
try {
BasicDBObject whereQuery = new BasicDBObject();
whereQuery.put("instance", dataVersion);
DBCursor curr = feedCollection.find(whereQuery); //.sort(new BasicDBObject(mongoDBSetting.maxIdField.replace("$", ""), -1)).limit(1);
//AggregationOutput cursor = feedCollection.find().sort(""); //.aggregate(Arrays.asList(query));
while (curr.hasNext()) {
// String mid = curr.next().get(mongoDBSetting.maxIdField.replace("$", "")).toString();
//
// mid = mid.replace("", "");
maxJobId = curr.next().get(mongoDBSetting.maxIdField.replace("$", "")).toString(); //Integer.parseInt(mid);
}
/* for (DBObject result : curr.results()) {
maxJobId = Integer.parseInt(result.get(mongoDBSetting.maxIdField.replace("$", "")).toString());
break;
}*/
}
catch (Exception ex) {
AppLogger.EventLogger.error("Exception occured selecting maxid in mongoDB. Exception -" + ex.getMessage().toString());
throw new Exception(ex.getMessage().toString());
}
return maxJobId;
}
Please let me know where I am missing. I have added method that needs test case.