Groovy 为什么斯波克认为我的数据提供商没有数据?

Groovy 为什么斯波克认为我的数据提供商没有数据?,groovy,spock,Groovy,Spock,我刚刚编写了自己的数据提供程序,它应该以块的形式读取一个文件,并将其提供给我的spock规范。 在调试时,next()方法返回正确的批处理,如果读取器无法读取更多行,hasNext()将返回false。 但我得到了这个例外:SpockExecutionException:数据提供程序没有数据 这是我的提供者和我的功能 class DumpProvider implements Iterable<ArrayList<String>> { private File fileH

我刚刚编写了自己的数据提供程序,它应该以块的形式读取一个文件,并将其提供给我的spock规范。 在调试时,next()方法返回正确的批处理,如果读取器无法读取更多行,hasNext()将返回false。 但我得到了这个例外:
SpockExecutionException:数据提供程序没有数据

这是我的提供者和我的功能

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();
        }
    }
}
}
类DumpProvider实现Iterable{
私有文件句柄
专用BufferedReader文件读取器
私有ArrayList currentBatch=新ArrayList()
私有整数块大小
私有布尔值hasNext=true
DumpProvider(字符串路径文件,int chunksize){
this.chunksize=chunksize
this.fileHandle=新文件(pathToFile)
this.fileReader=this.fileHandle.newReader()
}
@凌驾
迭代器迭代器(){
新迭代器(){
@凌驾
布尔hasNext(){
如果(下一步){
String nextLine=fileReader.readLine()
如果(下一行!=null){
currentBatch.push(下一行)
}否则{
hasNext=false
fileReader.close()
fileHandle=null
}
}
返回hasNext
}
@凌驾
arraylistnext(){
(chunksize-currentBatch.size())。次{
String line=fileReader.readLine()
如果(行!=null){
currentBatch.push(行)
}
}
def batch=新阵列列表(当前批次)
currentBatch=new ArrayList()
返回批次
}
@凌驾
作废删除(){
抛出新的UnsupportedOperationException();
}
}
}
}
斯波克特征

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

    then:
    println "hello"

    where:
    profileJSONStrings << dataProvider
}
def“小导入”(){
什么时候:
打印“测试”
println profileJSONStrings
connector.insertMultiple(ProfileJSonString作为ArrayList)
然后:
打印“你好”
哪里:

ProfileJSonString你能把上面的问题变成一个人们可以运行的问题吗?你在哪里创建数据提供者?它在看什么文件?批量大小是多少?文件的内容是什么?迭代器自己工作吗?我同意Tim。更具体地说:请学习并提供一个。谢谢。我不知道你在哪里实例化了Spock测试方法中的umpProvider。我假设dataProvider是实例。Spock在您可以创建测试类状态的地方很挑剔。它应该在以下情况下在测试方法中创建:block或通过setup()或setupSpec()。请参阅Fixture Methods一节