我们是否需要测试一个公共方法的失败场景,该场景会因Exception而失败?

use*_*500 3 unit-testing scala scalatest scalamock

我有一个Employee 定义的案例类,因为case class Employee(.........fields.....) 我有一个方法说

def getEmployees(organization: String): Future[Seq[Employee]] = {
   val result = employeeClient.getAllEmployees(organization)

   // some logic on this list of Employees received from the 
   client and manipulate it to get  finalListOfEmployees and return it 
   to caller of `getEmployees`//

  finalListOfEmployees

//end //
}
Run Code Online (Sandbox Code Playgroud)

现在,我getEmployees使用scala模拟进行测试。我不处理来自getEmployees或不recovering来自它的异常。这意味着出现在getAllEmployees客户端方法中的异常将传回给的调用者getEmployees

现在的问题是,我们需要测试这方面吗?

我的意思是以下测试会增加任何价值吗?

"Fail with future" in {  (mockEmployeeClient.getAllEmployees_).expects("SomeOrganization").returning(Future.failed(new Exception("failed"))
          getEmployees("SomeOrganization).failed.futureValue.getMessage shouldBe "failed"
}
Run Code Online (Sandbox Code Playgroud)

Mar*_*lic 5

我认为我们应该进行测试,因为这里的语义似乎是getEmployees期望失败由失败者表示的调用者Future。现在,考虑如果有人进行重构getEmployees,从而employeeClient.getAllEmployees(organization)通过返回一个空列表Future(Nil)来恢复故障,将会发生什么情况呢?

def getEmployees(organization: String): Future[Seq[Employee]] = {
 val result = employeeClient.getAllEmployees(organization)
 result.recover { case e => List.empty[Employee] }
 ...
}
Run Code Online (Sandbox Code Playgroud)

一切都会像以前一样编译良好,但是突然之间语义就大不相同了。单元测试可以捕获语义上的这种变化,并提示我们删除重构或getEmployees适当地更新调用方。