-1

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.

demongolem
  • 9,474
  • 36
  • 90
  • 105
Vigneshwaran
  • 782
  • 2
  • 7
  • 22
  • You're calling `find(obj)` on `feedCollection` which was never initialized, what did you expect? – crizzis Jun 06 '17 at 15:38

1 Answers1

0

You record a mock behavior on feedCollection that is not actually mocked

when(feedCollection.find(obj)).thenReturn((DBCursor) iterable);

The object is null.

So feedCollection.find(obj) throws a nullPointerException.

Actually you have mocked these two objects :

 curr =  mock(DBCursor.class);
 BasicDBObject obj = mock(BasicDBObject.class);

Besides mocking an object is the first step to mock a method but to get it working, the mock object should be associated to the object under test.
And it is not the case. You create the MongoQuerier object you want to test but you don't set mocks object to it.

MongoQuerier query = new MongoQuerier();

You should rather create an instance of the object to test with mocked object.

For example in this way to associate to it the mocked object(s).

MongoQuerier query = new MongoQuerier(mockObject, mockObject2, ...);
davidxxx
  • 125,838
  • 23
  • 214
  • 215
  • I have added mock for feedCollection I can able to overcome Null pointer Exception but Mock not works in Method am trying write junit ,I have added method where i am writing testcase @davidxxx – Vigneshwaran Jun 06 '17 at 15:39
  • As explained, the mock object should also be associated to the object under test. Create a mock object it is only the first step. – davidxxx Jun 06 '17 at 15:47