1

Im trying to mock scala call-by name method in mockito. But running into this error.

This exception may occur if matchers are combined with raw values: //incorrect: someMethod(anyObject(), "raw String"); When using matchers, all arguments have to be provided by matchers. For example: //correct: someMethod(anyObject(), eq("String by matcher"));

Any suggestion would be appreciated. Thanks!

Here is the sample code and test file: Here Im trying to mock createCommand function. and give a mock so I can verify that execute is called or not.

  package com.example

  class Command(key: String, func: => Long) {
    def execute(): Long = {
      println("Command.execute")
      println("key = " + key)
      println("func = " + func)
      func
    }

  }

  class CacheHelper {


    def createCommand(cacheKey: String, func: => Long): Command = {
      println("cacheKey = " + cacheKey)
      println("func = " + func)
      new Command(cacheKey, func)
      //    Mock this method
    }

    def getOrElse(cacheKey: String)(func: => Long): Long = {

      println("circuitBreakerEnabled = " + isCircuitBreakerEnabled)
      if (isCircuitBreakerEnabled) {
        val createCommand1: Command = createCommand(cacheKey, func)
        println("createCommand1 = " + createCommand1)
        createCommand1.execute()
      }
      else {
        util.Random.nextInt()
      }
    }

    def isCircuitBreakerEnabled: Boolean = {
      println("CacheHelper.isCircuitBreakerEnabled")
      false
    }
  }

  import com.example.{CacheHelper, Command}
  import org.mockito.Matchers._
  import org.mockito.Mockito._
  import org.scalatest.{Matchers, _}
  import org.scalatest.mock.MockitoSugar

  class ExampleSpec extends FlatSpec with Matchers with BeforeAndAfter with MockitoSugar {

    "it" should "call commands execute" in {
      val cacheHelper: CacheHelper = new CacheHelper
      val commandMock: Command = mock[Command]
      val spyCacheHelper = spy(cacheHelper)

      when(spyCacheHelper.isCircuitBreakerEnabled).thenReturn(true)
      when(spyCacheHelper.createCommand(any(), anyLong())).thenReturn(commandMock)

      val result: Long = spyCacheHelper.getOrElse("key")(1L)
      println("result = " + result)
      verify(commandMock).execute()

    }

  }
mohan
  • 41
  • 1
  • 4
  • You'll need to include more of the error stack trace for someone to help you answer this question. – Gavin Schulz Nov 03 '15 at 09:12
  • you've send a value as parameter `anyLong()` but instead you have to send some function from `Unit => Long` – vvg Nov 03 '15 at 09:55
  • Im getting the error with `Unit=>Long` since this is call-by name params, not functional arg. – mohan Nov 03 '15 at 16:09
  • Here are the dependencies if some one wants give it a try: `libraryDependencies += "org.mockito" % "mockito-all" % "1.10.19" % "test" libraryDependencies += "org.scalatest" %% "scalatest" % "2.2.4" % "test"` – mohan Nov 03 '15 at 16:10
  • keep an on this one: https://github.com/mockito/mockito/issues/1134 – stsatlantis Jul 03 '18 at 15:41

2 Answers2

3

Can't do it with Mockito: Verifying by-name parameters in Mockito

Should be able to do it with scalamock: How to mock a call-by-name argument (like getOrElse) using ScalaMock?

Community
  • 1
  • 1
Alvaro Carrasco
  • 6,103
  • 16
  • 24
2

You can use https://github.com/mockito/mockito-scala

import com.example.{CacheHelper, Command}
import org.mockito.ArgumentMatchersSugar._
import org.mockito.IdiomaticMockito
import org.scalatest.{Matchers, _}

  class ExampleSpec extends FlatSpec with Matchers with BeforeAndAfter with IdiomaticMockito {

    "it" should "call commands execute" in {
      val cacheHelper: CacheHelper = new CacheHelper
      val commandMock: Command = mock[Command]
      val spyCacheHelper = spy(cacheHelper)

      spyCacheHelper.isCircuitBreakerEnabled shouldReturn true
      spyCacheHelper.createCommand(any[String], any[Long]) shouldReturn commandMock 

      val result: Long = spyCacheHelper.getOrElse("key")(1L)
      println("result = " + result)
      verify(commandMock).execute()

    }

  }