忽略对 Mock 方法的调用的 Spock 交互验证

Spock interaction verification ignoring call to Mock method

以下 Spock 测试未计算对 Mock 方法的调用:

def setup() {
    mojo = new PactCreateVersionTagMojo()
    mojo.pactBrokerUrl = 'http://broker:1234'
    mojo.pacticipant = 'test'
    mojo.pacticipantVersion = '1234'
    mojo.tag = 'testTag'
}

def 'calls pact broker client with mandatory arguments'() {
    given:
    mojo.brokerClient = Mock(PactBrokerClient)

    when:
    mojo.execute()

    then:
    notThrown(MojoExecutionException)
    1 * mojo.brokerClient.createVersionTag(
            'test', '1234', 'testTag')
}

你可以找到它here

去除参数验证码的 SUT 代码是:

class PactCreateVersionTagMojo : PactBaseMojo() {

  override fun execute() {
    ...
    createVersionTag()
  } 

private fun createVersionTag() =
      brokerClient!!.createVersionTag(pacticipant!!, pacticipantVersion.orEmpty(), tag.orEmpty())

你可以找到它here

错误如下:

我在同一个项目上有一个非常相似的例子,它通过得很好:

 def 'passes optional parameters to the pact broker client'() {
    given:
    mojo.latest = 'true'
    mojo.to = 'prod'
    mojo.brokerClient = Mock(PactBrokerClient)

    when:
    mojo.execute()

    then:
    notThrown(MojoExecutionException)
    1 * mojo.brokerClient.canIDeploy('test', '1234',
      new Latest.UseLatest(true), 'prod') >> new CanIDeployResult(true, '', '')
  }

override fun execute() {
    ...

    val result = brokerClient!!.canIDeploy(pacticipant!!, pacticipantVersion.orEmpty(), latest, to)
}

你可以在上面找到测试here and the SUT here

我调查了测试期间发生的调用,似乎符合预期。 此外,我尝试创建带有通配符参数约束的验证,但它仍然不起作用。

在我看来,我的测试配置有误,但我看不出通过的测试和失败的测试之间的区别。

您的 fun createVersionTag(..) 看起来像这样:

  fun createVersionTag(
      pacticipant: String,
      pacticipantVersion: String,
      tag: String) {
  }

我不会说 Kotlin,但我认为你应该 open 方法,否则它是最终的,这意味着它不能被子类覆盖,因此不能被常规方法模拟或存根。这也是与open fun canIDeploy(..).

的区别