java - Mockito Scala 重载方法值 thenReturn 和替代方案

标签 java scala types mockito

我是 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/

相关文章:

java - 尝试使用 Return 语句进行 Catch 和 Final 操作

java - 使用 Guice 为参数注释编写拦截器

scala - Scala 中的子类型和类型参数

java - 什么是 lub(null, Double)?

java - 在 Liferay 6.2 中使用 ActionMapping/RequestMapping 时,请求中的表单数据为空

java - 在文件系统中找不到为log4j生成的文件日志

scala - 使用 Apache Buildr 编译 Scala 2.8.x 代码

scala - Scala 中的自增 (++) 运算符

types - Agda 中列表的定义

typescript - 如何在不声明键类型的情况下声明对象值类型?