0

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
}
peetzweg
  • 63
  • 4
  • 2
    Can you form the above into a question people can run? Where do you create the data provider? What file is it looking at? What is the batch size? What are the contents of the file? Does the Iterator work on it's own? – tim_yates Apr 11 '17 at 15:02
  • 2
    I agree to Tim. More specifically: Please learn [how to ask a question on SO](http://stackoverflow.com/help/how-to-ask) and provide a [minimal, complete, and verifiable example](http://stackoverflow.com/help/mcve). Thank you. – kriegaex Apr 11 '17 at 21:59
  • I don't see where you instantiate DumpProvider in your Spock test method. I'm assuming dataProvider is the instance. Spock is picky where you can create state of a test class. It should be created in either the test method when: block or via setup() or setupSpec(). Please see http://spockframework.org/spock/docs/1.1-rc-3/spock_primer.html Section Fixture Methods – Jason Heithoff Apr 13 '17 at 00:29

0 Answers0