我是 Scala 新手,正在为 Scala 中的 Play 应用程序编写一些测试。 Play 应用程序已经用 Java 编写。
我有一个 RefreshService,它有一个公共(public)方法流程
public RefreshResponse process(RefreshRequest request) throws Exception {
return this.oauthService.token(request.oauthUrl, request.clientId, request.clientSecret, request.refreshToken)
.thenCompose(oauthToken -> this.processHelper(request.withOAuthToken(oauthToken)))
.get();
}
其中操作在另一个包中定义为 POJO
我已经根据 Scala 指南编写了测试
当尝试模拟此服务时,我使用了以下代码
var mockRefreshService = mock[RefreshService]
when(mockRefreshService.process(_: RefreshRequest))
thenReturn (new RefreshResponse)
我从 Scala 中收到以下编译器错误,并且无法弄清楚类型为何会不明确
[error] /home/joey/Projects/sntdb/test/controllers/ApiControllerSpec.scala:31: overloaded method value thenReturn with alternatives:
[error] (x$1: actions.RefreshRequest => actions.RefreshResponse,x$2: actions.RefreshRequest => actions.RefreshResponse*)org.mockito.stubbing.OngoingStubbing[actions.RefreshRequest => actions.RefreshResponse]
[error] (x$1: actions.RefreshRequest => actions.RefreshResponse)org.mockito.stubbing.OngoingStubbing[actions.RefreshRequest => actions.RefreshResponse]
[error] cannot be applied to (actions.RefreshResponse)
[error] when(mockRefreshService.process(_: RefreshRequest)) thenReturn (new RefreshResponse)
如果需要更多信息,请告诉我。否则,如果有人有任何想法,我们将不胜感激。
最佳答案
如果您使用mockito-scala
,您可以混合org.mockito.ArgumentMatchersSugar
特征,并且它将提供所有合适的特征,此外,任何
你不再需要指定类型,因为编译器会弄清楚
检查https://github.com/mockito/mockito-scala了解更多信息
免责声明:我开发了该库(但无论如何它是mockito套件的一部分)
关于java - Mockito Scala 重载方法值 thenReturn 和替代方案,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43904850/