我刚刚编写了自己的数据提供程序,它应该以块的形式读取文件并将其提供给我的spock规范。
调试next()方法时返回一个正确的批处理,如果读者不能再读取任何行,则hasNext()返回false。
但是我得到了这个例外:SpockExecutionException: Data provider has no data
这是我的提供商和我的功能
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功能
def "small import"() {
when:
println 'test'
println profileJSONStrings
connector.insertMultiple(profileJSONStrings as ArrayList<String>)
then:
println "hello"
where:
profileJSONStrings << dataProvider
}