As far as I know, Scala doesn't have checked exceptions i.e. I don't need to specify the exception a method will throw.
I have a method A
of class a
under test. It calls method B
of class b
. I want to test behavior when B
throws an exception.
class b{
def B()={...}
}
I have mocked B
when(mockB.B).thenThrow(new UserDoesNotExistException("exception"))
When I do this, I get error Checked exception is invalid for this method!
This answers explains w.r.t. Java
- throw checked Exceptions from mocks with Mockito
While changing UserDoesNotExistException
to RuntimeException
works for me, I am curious to know if it is possible for me to test by throwing UserDoesNotExistException
In my logic, A
has different paths depending on which type of exception is thrown, thus I need to throw specific exceptions from my tests rather than throwing generic RuntimeException
.