如何在Mockito中模拟Scala呼叫者名称 [英] How do you mock scala call-by name in Mockito

查看:129
本文介绍了如何在Mockito中模拟Scala呼叫者名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在Mockito中模拟Scala呼叫者名称方法.但是遇到了这个错误.

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

如果匹配器与原始值组合,则可能发生此异常: //不正确: someMethod(anyObject(),原始字符串");使用匹配器时,所有参数都必须由匹配器提供.例如: //正确的: someMethod(anyObject(),eq(匹配者的字符串"));

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!

这是示例代码和测试文件:在这里,我试图模拟 createCommand 函数.并进行模拟,这样我就可以验证是否调用了 execute .

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

    }

  }

推荐答案

使用Mockito无法做到:

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

应该可以用scalamock做到这一点: 如何模拟呼叫-使用ScalaMock命名参数(例如getOrElse)?

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

这篇关于如何在Mockito中模拟Scala呼叫者名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆