groovy - spock possible bug? -
here're of base interfaces:
public interface parsingservice { boolean isqualifiednode(resource resource) } interface indexbuilder { boolean buildsingleresource(resource resource) } // osgi component. implementation indexbuilder @component( immediate = true ) @service class solrindexbuilder implements indexbuilder { list<parsingservice> parsers @override public boolean buildsingleresource(resource subject) { boolean success = true solrclient client = serverconfiguration.getsolrclient() (parsingservice parser : parsers) { try{ if(parser.isqualifiednode(subject)) { client.commit() } else { log.debug(" ${subject.path} not qualified parsing parser: ${parser.servicesummary}") continue //continue trying next parser } } catch (exception e) { success = false log.error("error while parsing resource: ${subject} parser: ${parser.servicesummary}") continue //continue trying next parser } } return success } }
and here's corresponding spock unit-test
class solrindexbuilderspecification extends specification { solrindexbuilder indexbuilder solrclient mockclient = mock() solrserverconfiguration mockserverconfiguration = mock() parsingservice parser = mock() parsingservice parser_page = mock() parsingservice parser_asset = mock() def setup(){ indexbuilder = new solrindexbuilder() mockserverconfiguration.getsolrclient() >> mockclient } def "testing buildsingleresource: parsers qualify node, process node under scrutiny"() { setup: resource pageresource = mock() parser.isqualifiednode(_) >> false parser_page.isqualifiednode(_) >> true // why not return true in method under test? indexbuilder.parsers = arrays.aslist(parser,parser_page) when: indexbuilder.buildsingleresource(pageresource) then: 1 * parser.isqualifiednode(pageresource) 1 * parser_page.isqualifiednode(pageresource) /* * todo: troubleshoot below when have time. * parser supposed invoke mockclient.commit() call once. invocation clause: * 1 * mockclienct.commit() * * should yield true. however, doesn't hold. instead invocation clause: * 0 * mockclient.commit() * * holds true. */ 0 * mockclient.commit() // shouldn"t !! 1 of parsers should return true 'isqualifiednode(..)' } }
as can see in test under investigation: have 2 mocks 'parsingservice' interface defined. 'parser' , 'parser_page'. also, 1 of them designed return true when .isqualifiednode(..) method called. however, both of them, false returned. why ? can try recreate this. more look, more i'm convinced bug, unless don't understand fundamental spock!
get rid of
parser.isqualifiednode(_) >> false parser_page.isqualifiednode(_) >> true
and change
1 * parser.isqualifiednode(pageresource) 1 * parser_page.isqualifiednode(pageresource)
to
1 * parser.isqualifiednode(pageresource) >> false 1 * parser_page.isqualifiednode(pageresource) >> true
Comments
Post a Comment