I have a partial function as exceptionHandler
which matches the corresponding exception and throws accordingly.
I am supposed to write a test case for NoHostAvailableException
, but I am unable to throw the exception using mocking.
I have made already a mock server which makes embedded Cassandra down in Lagom.
This is the partial function.
private val handleException: PartialFunction[Throwable, Future[List[MonitoringData]]] = {
case noHostAvailableException: NoHostAvailableException => throw new CassandraNotAvailableException(TransportErrorCode
.fromHttp(Error.CassandraNotAvailableErrorCode), Error.ErrorMessageForCassandraNotAvailable)
case _ => throw new TransportException(TransportErrorCode.InternalServerError, Error.ErrorMessageForInternalServerError)
}
This is the test case.
"not be able to interact with the database in" {
when(mockReadDAO.getData)
.thenThrow(NoHostAvailableException)
assert(thrown.isInstanceOf[NoHostAvailableException])
}
The compiler doesn't take NoHostAvailableException
as value.