I have a method that returns a Try
object:
def doSomething(p: SomeParam): Try[Something] = {
// code
}
I now want to test this with ScalaTest. Currently I am doing it like this:
"My try method" should "succeed" in {
val maybeRes = doSomething(SomeParam("foo"))
maybeRes.isSuccess shouldBe true
val res = maybeRes.get
res.bar shouldBe "moo"
}
However checking for isSuccess
to be true
looks a bit clumsy because for Options and Sequences there are things like should be(empty)
and shouldNot be(empty)
. I cannot find anything like should be(successful)
.
Does this exist or is my approach really the way to go?
Alternatively
Another possibility is to do
This will give a message indicating the
Try
was not a success, instead of throwing the exception inmaybeRes.get
.The analog exist for
Option
,Either
andPartialFunction
(using the relevant import)Just check to see that it is the success type with your return value: