2016-11-10 1 views
0

바로 여기에 내 기본 인터페이스의 일부 : 여기spock 가능한 버그?

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 
    } 
} 

그리고 해당 스팍의 단위 테스트

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 '인터페이스가 정의되었습니다. 그것들은 '파서'와 '파서 ​​페이지'입니다. 또한 이들 중 하나는 .isQualifiedNode (..) 메서드가 호출 될 때 true를 반환하도록 설계되었습니다. 그러나 둘 모두에 대해 false 만 반환됩니다. 왜 ? 누군가가 이것을 재현 할 수 있습니까? 더 많이 보았을 때, 내가 spock에 대한 근본을 이해하지 못한다면, 나는 그것이 버그라고 확신합니다!

답변

2

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 
+0

감사를 변경할 수 있지만, 또한 내가하기 이것은 spock 문서에 기록 된 것을 발견했습니다. –