Spock: mock a method with varargs

后端 未结 1 698
一向
一向 2021-01-20 14:06

This question is an offshoot of this Q&A: Test Groovy class that uses System.console()

The problem:

Spock framework incorrectly checks a

相关标签:
1条回答
  • 2021-01-20 14:32

    You can avoid having to explain the varargs by using Mock() instead of GroovyMock().

    In general, when writing Spock specifications you should prefer Spock's Mock object over Groovy's GroovyMock unless you intend to use some of the unique functionality from GroovyMock. Reference

    Swapping out Mock for GroovyMock allows you to avoid the verbose Object params:

            setup:
                def consoleM = Mock(IConsole)
                1 * consoleM.readline(_) >> "hey"
    
            when:
                def result = consoleM.readline("prompt")
    
            then:
                result == "hey"
    
    0 讨论(0)
提交回复
热议问题