测试用例没有被测试方法抛出异常

问题描述

这是我的测试方法。我想测试它是否接收到异常,然后对其进行处理(如果需要)并将其进一步抛出。

def handleNewUserTransaction(profile:UserProfile) = Future{ 
    logger.trace(s"handling new user transaction with profile ${profile}")
    val (userKeys,user,emailToken) = getParametersTohandleNewUserTransaction(profile)
    logger.trace(s"handing new user transaction with values ${userKeys},${user},${emailToken}")
    try{
      userTransactionDatabaseService.handleNewUserDatabaseTransaction(userKeys,emailToken)
      sendEmailUsingConfirmationToken(user,emailToken)
      //checktest-user is added on success
      true
    } catch {
      case exception:UserNotConfirmedException => {
        //checktest-email is sent for unconfirmed user
        logger.trace(s"user not confirmed. Will resent email")
        sendEmailToUnconfirmedUser(emailToken,exception.user)
        throw exception
      }
        //checktest-throw exception if error
      case exception:Throwable => {
        logger.trace(s"got exception ${exception}")
        throw exception
      }
    }
  }

我正在通过此测试用例来模拟异常情况

"throw exception if error" in {
      val servicesTestEnv = new ServicesTestEnv(components=components)
      //val exception = UnconfirmedUserException().asInstanceOf[RuntimeException]
      val userTransactionService = new UserTransactionService(servicesTestEnv.userTestEnv.mockMailerService,components.configuration,servicesTestEnv.mockUserTransactionDBService,servicesTestEnv.testEnv.mockHelperMethods)
      when(servicesTestEnv.mockUserTransactionDBService.handleNewUserDatabaseTransaction(ArgumentMatchers.any[UserKeys],ArgumentMatchers.any[User],ArgumentMatchers.any[UserToken]))
        .thenAnswer(invocation => {throw UserDoesNotExistException()})
      when(servicesTestEnv.testEnv.mockHelperMethods.generateUserKeysFromEmail(ArgumentMatchers.any[String]))
        .thenReturn(servicesTestEnv.userTestEnv.userKeys)
      when(servicesTestEnv.testEnv.mockHelperMethods.hashPassword(ArgumentMatchers.any[String]))
        .thenReturn(servicesTestEnv.testEnv.passwordInfo)
      when(servicesTestEnv.testEnv.mockHelperMethods.bucketIDFromEmail(ArgumentMatchers.any[String]))
        .thenReturn(1)
      when(servicesTestEnv.testEnv.mockHelperMethods.getUniqueID())
        .thenReturn(servicesTestEnv.testEnv.user.id)
      when(servicesTestEnv.testEnv.mockHelperMethods.generateEmailTokenForUser(ArgumentMatchers.any[User],ArgumentMatchers.any[UserTokenType.TokenType]))
        .thenReturn(servicesTestEnv.userTestEnv.userToken)
      intercept[UserDoesNotExistException](userTransactionService.handleNewUserTransaction(UserProfile(None,servicesTestEnv.testEnv.externalUserProfile)))
      verify(servicesTestEnv.mockUserTransactionDBService,times(1)).handleNewUserDatabaseTransaction(ArgumentMatchers.any[UserKeys],ArgumentMatchers.any[UserToken])
      verify(servicesTestEnv.userTestEnv.mockMailerService,times(0)).sendEmail(ArgumentMatchers.any[String],ArgumentMatchers.any[String],ArgumentMatchers.any[List[String]],ArgumentMatchers.any[Option[String]],ArgumentMatchers.any[Option[String]])

    }

但是测试用例失败,原因是-Expected exception utilities.UserDoesNotExistException to be thrown,but no exception was thrown

我检查了踪迹,可以看到异常被模拟抛出-2020-08-17 08:22:27,461 [TRACE] from services.UserTransactionService in scala-execution-context-global-33 - got exception utilities.UserDoesNotExistException: Invalid user

为什么测试用例失败?

解决方法

愚蠢的错误。该函数正在返回Future。测试用例不是在等待Future完成。

val exception = intercept[UserDoesNotExistException](await[Boolean](userTransactionService.handleNewUserTransaction(UserProfile(None,servicesTestEnv.testEnv.externalUserProfile)))(Timeout(Duration(5000,"millis"))))