@Test(expected = IllegalStateException.class)
public void testNewResultSet_Iterator_Exception_Close() throws Exception{

    // Mocks
    ConnectionManager connectionManager = mock(ConnectionManager.class);
    Connection connection = mock(Connection.class);
    PreparedStatement preparedStatement = 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.prepareStatement("SELECT DISTINCT objectKey FROM " + TABLE_NAME + " WHERE value = ?;")).thenReturn(preparedStatement);
    when(preparedStatement.executeQuery()).thenReturn(resultSet);
    when(resultSet.getStatement()).thenReturn(preparedStatement);
    when(resultSet.next()).thenReturn(true).thenReturn(true).thenReturn(false);
    when(resultSet.getInt(1)).thenReturn(1).thenThrow(new SQLException("SQL exception"));
    when(idToObject.getValue(eq(1), anyQueryOptions())).thenReturn(data.get(0));
    // Iterator
    try {
        ResultSet<Car> carsWithAbs = new SQLiteIndex<String, Car, Integer>(
                Car.FEATURES,
                OBJECT_TO_ID,
                idToObject,
                "")
                .retrieve(equal(Car.FEATURES, "abs"), createQueryOptions(connectionManager));
        assertNotNull(carsWithAbs);
        Iterator<Car> carsWithAbsIterator = carsWithAbs.iterator();
        assertNotNull(carsWithAbsIterator.next());
        carsWithAbsIterator.next();// Should throw exception!
    }finally {
        verify(connection, times(0)).close(); // Connection should be left open
        verify(preparedStatement, times(1)).close();
        verify(resultSet, times(1)).close();
    }
}