如何在Mockito和Scala中使用隐式匹配器对方法调用进行存根 [英] How to stub a method call with an implicit matcher in Mockito and Scala

查看:66
本文介绍了如何在Mockito和Scala中使用隐式匹配器对方法调用进行存根的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序代码使用AService

My application code uses AService

trait AService {
    def registerNewUser (username: String)(implicit tenant: Tenant): Future[Response]
}

注册一个新用户.租户类是一个简单的案例类:

to register a new user. Class Tenant is a simple case class:

case class Tenant(val vstNumber:String, val divisionNumber:String) 

特质AServiceMock通过模拟AService的版本来模仿注册逻辑

Trait AServiceMock mimics the registration logic by using a mocked version of AService

trait AServiceMock {
  def registrationService = {
    val service = mock[AService]
    service.registerNewUser(anyString) returns Future(fixedResponse)
    service
  }
}

现在,每当在AService上调用registerNewUser时,响应将为"fixedResponse"(在其他地方定义).

Iow whenever registerNewUser is called on AService the response will be "fixedResponse" (defined elsewhere).

我的问题是,如何将隐式租户参数定义为类似于anyString的模拟匹配器?

My question is, how do I define the implicit tenant-parameter as a mockito matcher like anyString?

顺便说一句.我正在将Mockito与Specs2(和Play2)配合使用

btw. I'm using Mockito with Specs2 (and Play2)

推荐答案

有时您必须先在SO上发布,才能得出完全显而易见的答案(duhh):

Sometimes you have to post on SO first to come up with the completely obvious answer (duhh):

service.registerNewUser(anyString)(any[Tenant]) returns Future(fixedResponse)

这篇关于如何在Mockito和Scala中使用隐式匹配器对方法调用进行存根的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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