首页 > 解决方案 > 当您的方法在测试中返回 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吗?

标签: scalascalatest

解决方案


推荐阅读