Why does Spock think my Data Provider has no data?

旧巷老猫 提交于 2019-12-24 18:43:16

问题


I just wrote my own data Provider, which should read a file in chunks and provides it to my spock specification. While debugging the next() method returns a proper batch and the hasNext() returns false if the reader can not read any more lines. But I get this exception: SpockExecutionException: Data provider has no data

Here is my Provider and my feature

class DumpProvider implements Iterable<ArrayList<String>> {
private File fileHandle
private BufferedReader fileReader
private ArrayList<String> currentBatch = new ArrayList<String>()
private int chunksize
private boolean hasNext = true


DumpProvider(String pathToFile, int chunksize) {
    this.chunksize = chunksize
    this.fileHandle = new File(pathToFile)
    this.fileReader = this.fileHandle.newReader()
}

@Override
Iterator iterator() {
    new Iterator<ArrayList<String>>() {
        @Override
        boolean hasNext() {
            if (hasNext) {
                String nextLine = fileReader.readLine()
                if (nextLine != null) {
                    currentBatch.push(nextLine)
                } else {
                    hasNext = false
                    fileReader.close()
                    fileHandle = null
                }
            }
            return hasNext
        }


        @Override
        ArrayList<String> next() {
            (chunksize - currentBatch.size()).times {
                String line = fileReader.readLine()
                if (line != null) {
                    currentBatch.push(line)
                }
            }
            def batch = new ArrayList<String>(currentBatch)
            currentBatch = new ArrayList<String>()
            return batch
        }

        @Override
        void remove() {
            throw new UnsupportedOperationException();
        }
    }
}
}

Spock Feature

def "small import"() {
    when:
    println 'test'
    println profileJSONStrings
    connector.insertMultiple(profileJSONStrings as ArrayList<String>)

    then:
    println "hello"

    where:
    profileJSONStrings << dataProvider
}

来源:https://stackoverflow.com/questions/43348388/why-does-spock-think-my-data-provider-has-no-data

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!