title: Mocking DeleteResult of MongoDB using Mockito in Java
body: <p>The unit test below is not able to mock DeleteResult for the Java code being tested. Getting a NullPointerException. I'm running the test on JUnit. Is it something to do with the Filters in the delete statement?</p>
  
  <pre><code>    @InjectMocks
      DBConnection mongoConnect;
  
      @Mock
      MongoClient mockClient;
  
      @Mock
      MongoCollection&lt;Document&gt; mockCollection;
  
      @Mock
      MongoDatabase mockDB;
  
      @Mock
      LinkedList&lt;String&gt; mockArrList;
  
      @Mock
      MongoIterable&lt;String&gt; mongoIter;
  
      @Mock
      DeleteResult mockDeleteResult;
  
      @SuppressWarnings("unchecked")
      @Test
      public void deleteDocTest1() {
  
          Mockito.when(mockClient.getDatabase(Mockito.anyString())).thenReturn(mockDB);       
  
          MongoIterable&lt;String&gt; mongoIter = Mockito.mock(MongoIterable.class);
          Mockito.when(mockDB.listCollectionNames()).thenReturn(mongoIter);       
          Mockito.when(mongoIter.into(new LinkedList&lt;String&gt;())).thenReturn(mockArrList);
          Mockito.when(mockArrList.contains(Mockito.anyString())).thenReturn(true);   
          Mockito.when(mockDB.getCollection(Mockito.anyString())).thenReturn(mockCollection);
          Mockito.when(mockCollection.deleteOne(Filters.and(Filters.eq("aid", "TS123"), 
                  Filters.eq("year", "2018"),
                  Filters.eq("position", "testCases"))))
                  .thenReturn(mockDeleteResult);
          Mockito.when(mockDeleteResult.getDeletedCount()).thenReturn(1L);
  
          String msg = mongoConnect.deleteDocument("TS123", "testCases", "2018");
          assertEquals("Delete Successful", msg);     
  
      }
  </code></pre>
  
  <p>The code being tested just has to delete a record if the keys match, and return a warning if there is no such record. The method below, which is being tested, is part of DBCollection class:</p>
  
  <pre><code>public String deleteDocument(String aId, String collection, String year) {
  
          MongoDatabase database = mongoClient.getDatabase(databaseName);
  
          //checking if collection is present in the DB
          boolean collectionExists = database.listCollectionNames().into(new LinkedList&lt;String&gt;())
                  .contains(collection);
  
          if(collectionExists) {
              MongoCollection&lt;Document&gt; collectionDocs = database.getCollection(collection);
              System.out.println(assoId+" "+collection+" "+year);         
              DeleteResult deleteResult = collectionDocs.deleteOne(Filters.and(Filters.eq("aid", aId), Filters.eq("year",year), Filters.eq("position",collection)));
              if(deleteResult.getDeletedCount() == 0) //the ERROR is at this line
                  return "Delete: record does not exist";
          }else {
              return "Delete: record does not exist";
          }
          mongoClient.close();
          return "Successful Delete"; 
  
      }   
  </code></pre>
  
  <p>The stack trace for the error:</p>
  
  <pre><code>java.lang.NullPointerException
      at com.repo.repository.DBConnection.deleteDocument(DBConnection.java:103)
      at com.repo.form_upload.test.DBTest.deleteDocTest1(DBTest.java:138)
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
      at java.lang.reflect.Method.invoke(Unknown Source)
      at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
      at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
      at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
      at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
      at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
      at org.mockito.internal.junit.JUnitRule$1.evaluate(JUnitRule.java:16)
      at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
      at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
      at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
      at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
      at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
      at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
      at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
      at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
      at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
      at org.mockito.internal.runners.JUnit45AndHigherRunnerImpl.run(JUnit45AndHigherRunnerImpl.java:37)
      at org.mockito.runners.MockitoJUnitRunner.run(MockitoJUnitRunner.java:62)
      at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
      at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
      at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:538)
      at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:760)
      at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:460)
      at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:206)
  </code></pre>
  
  <p>Any ideas on what's the problem?</p>