Verifying that the no arg version of an overloaded function gets called in scalamock

96 views Asked by At

I am using scalamock and am trying to verify that the close() method in the RabbitMQ com.rabbitmq.client.Channel class is getting called. The problem is the close() method is overloaded with two options: close() and close(int, String). I want to verify that the no arg version is getting called.

I have tried the following code:

import com.rabbit.client.Channel
import org.scalatest.Wordspec
import org.scalamock.scalatest.MockFactory

class MessageSubscriberSpecs extends WordSpec with MockFactory {
  "A message subscriber" when {
    "closing a connection" should {
      // ... More test setup

      "close the underlying connection" in {
        val channelStub = stub[Channel]
        (channelStub.close _).verify()
      }
    }
  }
}

The line with verify() on it does not compile because the compiler is confused about which overloaded function to call.

How do I verify that the no arg version of an overloaded function gets called?

2

There are 2 answers

0
Akos Krivachy On

Yes, it's confused of which function it should create: Function0 or Function2 (i.e. a function with 0 or 2 parameters)

So let's solve this by explicitly defining a function with 0 parameters!

(() => channelStub.close()).verify()
0
ixiolite On

You can add an explicit type annotation for the function. In this case it's a no args method with a void return type, so it would be:

(channelStub.close _: () => Unit).verify()

likewise to match the other overloaded signature, it would be

(channelStub.close _: (Int,String) => Unit).verify(*,*)