Spock: mock a method with varargs

那年仲夏 提交于 2019-12-02 00:44:58

问题


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

The problem:

Spock framework incorrectly checks arguments of the mocked method with varargs.

Steps to reproduce:

1) Create groovy project

2) Create an interface:

interface IConsole {
  String readLine(String fmt, Object ... args)
}

3) Create spock test:

class TestInputMethods extends Specification {

  def 'test console input'() {
    setup:
    def consoleMock = GroovyMock(IConsole)
    1 * consoleMock.readLine(_) >> 'validResponse'

    when:
    // here we get exception "wrong number of arguments":
    def testResult = consoleMock.readLine('testPrompt')

    then:
    testResult == 'validResponse'
  }
}

4) try running the test

Effect: test fails with exception "wrong number of arguments".

Workaround:

call to readLine is replaced with:

def testResult = consoleMock.readLine('testPrompt', [] as Object[])

then test completes successfully.

Question:

is there a better way to "explain" spock that the last argument of the mocked function is vararg and, as such, it could be omitted?


回答1:


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"


来源:https://stackoverflow.com/questions/20808189/spock-mock-a-method-with-varargs

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!