How do you mock scala call-by name in Mockito

前端 未结 2 965
闹比i
闹比i 2021-01-15 19:47

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 value

相关标签:
2条回答
  • 2021-01-15 19:57

    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()
    
        }
    
      }
    
    0 讨论(0)
  • 2021-01-15 20:07

    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?

    0 讨论(0)
提交回复
热议问题