How to stub a method call with an implicit matcher in Mockito and Scala

送分小仙女□ 提交于 2021-02-20 05:52:51

问题


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) 

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
  }
}

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

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

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


回答1:


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)


来源:https://stackoverflow.com/questions/30452317/how-to-stub-a-method-call-with-an-implicit-matcher-in-mockito-and-scala

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!