Spock:使用 GroovyMock 不需要的真实方法调用

Spock: unwanted real method call with GroovyMock

我有这样的测试

def "fileField should be set for each line batch"(){
    given:
    LuceneEngine le = new LuceneEngine()
    le.indexWriter = Mock( IndexWriter ){
        addDocument(_) >> null 
    }
    le.currentFilename  = 'dummy filename'
    le.fileField = GroovyMock( TextField )

    when:
    le.processLineBatch([ 'dummy text' ], 0 )

    then:
    1 * le.fileField.setStringValue( 'dummy filename' ) >> null         

}

app 方法如下所示:

def processLineBatch( List lineBatch, int deliveryNo ) {
    String lDocText = lineBatch.join( '\n' ).trim()
    textField.setStringValue( lDocText )
    fileField.setStringValue( currentFilename )
    indexWriter.addDocument( singleLDoc )
}

我必须对 TextField 使用 GroovyMock 因为 class 是 final

无论我做什么(我已经尝试了很多东西)实际方法 setStringValue 得到 运行 ... 然后生成一个 Exception 因为这是一个模拟.

有关信息,失败如下所示:

java.lang.NullPointerException
at org.apache.lucene.document.Field.setStringValue(Field.java:307)
at org.spockframework.mock.runtime.GroovyMockMetaClass.doInvokeMethod(GroovyMockMetaClass.java:86)
at org.spockframework.mock.runtime.GroovyMockMetaClass.invokeMethod(GroovyMockMetaClass.java:42)
at core.LuceneEngine.processLineBatch(lucene_functions.groovy:422)

... 其中第 422 行是行 fileField.setStringValue (...

这似乎与我对非 Groovy 模拟的预期相反。任何人都可以解释我的错误以及是否有解决方案?

NB TextField 在 Lucene 6 中是 here... 从中你可以 link 到 superclass Field 并看到 setStringValue是(非finalpublic void.

其实你基本上已经问过同样的问题了,你甚至接受了!您需要使用 global Groovy mock:

package de.scrum_master.Whosebug

import org.apache.lucene.document.TextField
import org.apache.lucene.index.IndexWriter
import org.apache.lucene.index.IndexableField

class LuceneEngine {
  TextField textField
  TextField fileField
  IndexWriter indexWriter
  String currentFilename
  Iterable<? extends IndexableField> singleLDoc

  def processLineBatch(List lineBatch, int deliveryNo) {
    String lDocText = lineBatch.join('\n').trim()
    textField.setStringValue(lDocText)
    fileField.setStringValue(currentFilename)
    indexWriter.addDocument(singleLDoc)
  }
}
package de.scrum_master.Whosebug

import org.apache.lucene.document.TextField
import org.apache.lucene.index.IndexWriter
import spock.lang.Specification

class LuceneEngineTest extends Specification {
  def "fileField should be set for each line batch"() {
    given:
    LuceneEngine le = new LuceneEngine()
    le.indexWriter = Mock(IndexWriter) {
      addDocument(_) >> null
    }
    le.currentFilename = 'dummy filename'
    // Assign this to le.textField or le.fileField if you like, it does not
    // make a difference because the Groovy mock is GLOBAL
    GroovyMock(TextField, global: true)

    when:
    le.processLineBatch(['dummy text'], 0)

    then:
    1 * le.fileField.setStringValue('dummy filename') >> null
  }
}

至于为什么要在这里使用全局模拟,我无法解释。这是Spock mailing list的问题。我又完成了你的工作并在那里发布了一个问题。