你如何在 Mockito 中模拟 scala 调用名称 [英] How do you mock scala call-by name in Mockito

查看:17
本文介绍了你如何在 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("String by matcher"));

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

    }

  }

推荐答案

你可以使用 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()

    }

  }

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

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