@Before
  @SuppressWarnings("unchecked")
  public void setUp() throws Exception {
    MongoDatabase db = mock(MongoDatabase.class);
    MongoCollection<Entity> collection = mock(MongoCollection.class);

    when(db.getCollection(anyString(), any(Class.class))).thenReturn(collection);
    when(collection.withCodecRegistry(any(CodecRegistry.class))).thenReturn(collection);

    RepositorySetup setup = RepositorySetup.builder().database(db)
            .executor(MoreExecutors.newDirectExecutorService())
            .gson(new GsonBuilder().registerTypeAdapterFactory(new GsonAdaptersEntity()).create())
            .build();

    this.repository = new EntityRepository(setup);

    FindIterable<Entity> iterable = mock(FindIterable.class);
    when(collection.find(any(Bson.class))).thenReturn(iterable);
    MongoCursor<Entity> cursor = mock(MongoCursor.class);
    when(iterable.iterator()).thenReturn(cursor);

    this.cursor = cursor;
  }
Set of tests to ensure mongo resources are properly closed.

@see MongoCursor#close()
@see <a href="https://docs.mongodb.com/v3.4/reference/method/cursor.close/">cursor.close</a>
mongo/test/org/immutables/mongo/fixture/MongoCursorIsClosedTest.java