I have a test like this
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
}
The app method looks like this:
def processLineBatch( List lineBatch, int deliveryNo ) {
String lDocText = lineBatch.join( '\n' ).trim()
textField.setStringValue( lDocText )
fileField.setStringValue( currentFilename )
indexWriter.addDocument( singleLDoc )
}
I have to use GroovyMock
for TextField
because the class is final
.
Whatever I do (and I've tried quite a few things) the actual method setStringValue
gets run ... which then generates an Exception
as this is a mock.
For info, the failure looks like this:
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)
... where line 422 is the line fileField.setStringValue (...
This seems contrary to what I'd expect with a non-Groovy mock. Can anyone explain what I've got wrong and whether there's a solution?
NB TextField
in Lucene 6 is here... from which you can link to superclass Field
and see that setStringValue
is (non-final
) public void
.