spock 可能的错误?
spock possible bug?
这是我的一些基本接口:
public interface ParsingService {
boolean isQualifiedNode(Resource resource)
}
interface IndexBuilder {
boolean buildSingleResource(Resource resource)
}
// An OSGI component. Also an implementation for 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()
for (ParsingService parser : parsers) {
try{
if(parser.isQualifiedNode(subject)) {
client.commit()
}
else {
log.debug(" ${subject.path} not qualified for parsing by parser: ${parser.serviceSummary}")
continue //continue to trying the next parser
}
} catch (Exception e) {
success = false
log.error("error while parsing resource: ${subject} by parser: ${parser.serviceSummary}")
continue //continue to trying the next parser
}
}
return success
}
}
这是相应的 spock 单元测试
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: only parsers who qualify a node, will process the node under scrutiny"() {
setup:
Resource pageResource = Mock()
parser.isQualifiedNode(_) >> false
parser_page.isQualifiedNode(_) >> true // WHY does this 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 you have the time.
* Parser is supposed to invoke mockClient.commit() call once. So the invocation clause:
* 1 * mockClienct.commit()
*
* should yield true. However, that doesn't hold. Instead the invocation clause:
* 0 * mockClient.commit()
*
* holds true.
*/
0 * mockClient.commit() // SHOULDN"T BE !! One of the parsers should return true for 'isQualifiedNode(..)'
}
}
正如您在正在调查的测试中看到的那样:我为 'ParsingService' 接口定义了两个 Mock。它们是 'parser' 和 'parser_page'。此外,其中一个被设计为 return true 时调用 .isQualifiedNode(..) 方法。但是,对于他们两个,只有 false 是 returned。为什么 ?有人可以尝试重新创建这个吗?越看越觉得是bug,除非我不懂spock的基础知识!
去掉
parser.isQualifiedNode(_) >> false
parser_page.isQualifiedNode(_) >> true
并改变
1 * parser.isQualifiedNode(pageResource)
1 * parser_page.isQualifiedNode(pageResource)
至
1 * parser.isQualifiedNode(pageResource) >> false
1 * parser_page.isQualifiedNode(pageResource) >> true
这是我的一些基本接口:
public interface ParsingService {
boolean isQualifiedNode(Resource resource)
}
interface IndexBuilder {
boolean buildSingleResource(Resource resource)
}
// An OSGI component. Also an implementation for 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()
for (ParsingService parser : parsers) {
try{
if(parser.isQualifiedNode(subject)) {
client.commit()
}
else {
log.debug(" ${subject.path} not qualified for parsing by parser: ${parser.serviceSummary}")
continue //continue to trying the next parser
}
} catch (Exception e) {
success = false
log.error("error while parsing resource: ${subject} by parser: ${parser.serviceSummary}")
continue //continue to trying the next parser
}
}
return success
}
}
这是相应的 spock 单元测试
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: only parsers who qualify a node, will process the node under scrutiny"() {
setup:
Resource pageResource = Mock()
parser.isQualifiedNode(_) >> false
parser_page.isQualifiedNode(_) >> true // WHY does this 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 you have the time.
* Parser is supposed to invoke mockClient.commit() call once. So the invocation clause:
* 1 * mockClienct.commit()
*
* should yield true. However, that doesn't hold. Instead the invocation clause:
* 0 * mockClient.commit()
*
* holds true.
*/
0 * mockClient.commit() // SHOULDN"T BE !! One of the parsers should return true for 'isQualifiedNode(..)'
}
}
正如您在正在调查的测试中看到的那样:我为 'ParsingService' 接口定义了两个 Mock。它们是 'parser' 和 'parser_page'。此外,其中一个被设计为 return true 时调用 .isQualifiedNode(..) 方法。但是,对于他们两个,只有 false 是 returned。为什么 ?有人可以尝试重新创建这个吗?越看越觉得是bug,除非我不懂spock的基础知识!
去掉
parser.isQualifiedNode(_) >> false
parser_page.isQualifiedNode(_) >> true
并改变
1 * parser.isQualifiedNode(pageResource)
1 * parser_page.isQualifiedNode(pageResource)
至
1 * parser.isQualifiedNode(pageResource) >> false
1 * parser_page.isQualifiedNode(pageResource) >> true