scala - 当您的方法在测试中返回 Future 时如何抛出异常?
问题描述
所以我有一个方法TokenService
叫做useToken
:
def useToken(token: String): Future[T] = {
// returns either an instance of T or throws a ServerException
}
这被包裹在另一个类中说AuthService
:
def useTokenForAuth(token: String): Future[AuthTokenData] = {
tokenService.useToken(token)
}
在我的测试中,我嘲笑useToken
抛出一个异常,如:
when(
mockTokenService.useToken("fake-token")
).thenThrow(new ServerException)
mockAuthService.useTokenForAuth("fake-token")
并且mockAuthService
不会抛出异常。可能是因为返回类型是 aFuture
吗?
解决方案
推荐阅读
- c# - 按年龄分组
- java - 在android中使用google map api时无法更新位置
- javascript - Javascript ISReachable 不起作用。说require没有定义
- php - 如何将用户 ID 保存在 laravel 中特定用户使用软删除删除的记录中
- python - 如何读取文件夹中的所有图像?
- bash - Bash 解析器在命令行中以什么顺序转义字符和拆分单词/标记?
- swift - 为具有失败响应的字典数组实现 Codable
- javascript - Retyped.chartist 用法不清楚。如何创建基本折线图?
- android - 从 Firestore 获取数据后,方法总是返回 null
- java - 内存分配 Java 异常