João Farias
João Farias

Reputation: 239

Spock interaction verification ignoring call to Mock method

The following Spock test is failing to not counting the call to the Mock method:

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

You can find it here.

The SUT code, removing the argument validation code, is:

class PactCreateVersionTagMojo : PactBaseMojo() {

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

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

You can find it here.

The error is as follows:

enter image description here

I have a very similar example on the same project that passes just fine:

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

You can find the test above here and the SUT here.

I have investigated the call that happens during the test, and it seems as expected. Additionally, I try to create the verification with wildcard argument constraints, but it still didn't work.

It seems to me that I have misconfigured my test, but I can't spot the difference between the test that passes and my failing test.

Upvotes: 0

Views: 350

Answers (1)

kriegaex
kriegaex

Reputation: 67317

Your fun createVersionTag(..) looks like this:

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

I do not speak Kotlin, but I think you ought to open the method because otherwise it is final, which means it cannot be overridden by a subclass and thus not be mocked or stubbed by conventional means. This is also the difference to open fun canIDeploy(..).

Upvotes: 1

Related Questions