@Test
public void testNewResultSet_FilterQuery_Close() throws Exception{

    // Mocks
    FilterQuery<Car, String> filterQuery = mockFilterQuery();
    ConnectionManager connectionManager = mock(ConnectionManager.class);
    Connection connection = mock(Connection.class);
    Statement statement = mock(PreparedStatement.class);
    java.sql.ResultSet resultSet = mock(java.sql.ResultSet.class);
    @SuppressWarnings("unchecked")
    SimpleAttribute<Integer, Car> idToObject = (SimpleAttribute<Integer, Car>)mock(SimpleAttribute.class);
    // Behaviour
    when(connectionManager.getConnection(any(SQLiteIndex.class), anyQueryOptions())).thenReturn(connection);
    when(connection.createStatement()).thenReturn(statement);
    when(statement.executeQuery("SELECT objectKey, value FROM " + TABLE_NAME + " ORDER BY objectKey;")).thenReturn(resultSet);
    when(resultSet.getStatement()).thenReturn(statement);
    when(resultSet.next()).thenReturn(true).thenReturn(true).thenReturn(true).thenReturn(true).thenReturn(true).thenReturn(true).thenReturn(false);
    when(resultSet.getInt(1)).thenReturn(1).thenReturn(1).thenReturn(2).thenReturn(3).thenReturn(4).thenReturn(5);
    when(resultSet.getString(2)).thenReturn("abs").thenReturn("gps").thenReturn("airbags").thenReturn("abs").thenReturn("").thenReturn("gps");
    when(idToObject.getValue(eq(1), anyQueryOptions())).thenReturn(data.get(0));
    when(idToObject.getValue(eq(3), anyQueryOptions())).thenReturn(data.get(2));
    when(idToObject.getValue(eq(5), anyQueryOptions())).thenReturn(data.get(4));
    // Iterator
    ResultSet<Car> carsWithAbs = new SQLiteIndex<String, Car, Integer>(
            Car.FEATURES,
            OBJECT_TO_ID,
            idToObject,
            "")
            .retrieve(filterQuery, createQueryOptions(connectionManager));
    assertNotNull(carsWithAbs);
    Iterator carsWithAbsIterator = carsWithAbs.iterator();
    assertTrue(carsWithAbsIterator.hasNext());
    assertNotNull(carsWithAbsIterator.next());
    // Do not continue with the iteration, but close
    carsWithAbs.close();
    verify(connection, times(0)).close(); // Connection should be left open
    verify(statement, times(1)).close();
    verify(resultSet, times(1)).close();
}