object slack extends Logging{
def sendSlackMessage(
channel: String,
message: String,
webhook: String
):Boolean = {
if(StringUtils.isNoneEmpty(channel, message, webhook)) {
val api = new SlackApi(webhook)
val slackMessage = new SlackMessage()
slackMessage.setChannel(channel)
slackMessage.setText(message)
val results: Try[Unit] = Try(api.call(slackMessage))
results match {
case Failure(exception) =>
logError("Invalid channel/webhook. Couldn't sent notification!", exception)
false
case Success(value) =>
true
}
}
else {
logError("Invalid parameters. Couldn't sent notification!")
false
}
}
}
I would like to know if it's possible to unit test sendSlackMessage()
. The challenge I'm facing right now is that I cannot figure out a way to mock new SlackApi()
and even if I mock, I don't know how to tell the function to use the mocked value to instantiate and make the api call.