matt tighe
03/29/2019, 9:13 PMacraWrapper.logAndThrow()
logs some exception information and then just rethrows the exception
outside of just tricking it by returning an invalid value, that isPavlo Liapota
03/29/2019, 9:16 PMlogAndThrow
be Nothing
should help, if I understood you correctly.matt tighe
03/29/2019, 9:17 PMmatt tighe
03/29/2019, 9:31 PMNothing
return type causes the rest of the test to be unreachablematt tighe
03/29/2019, 9:31 PMkarelpeeters
03/29/2019, 9:38 PMNothing
can never return and so everything after that is unreachable, isn't that what you want?matt tighe
03/29/2019, 10:05 PMfun doThing() {
try { thing() }
catch (err: Exception) {
acraWrapper.logAndThrow(err)
}
}
@Test(expected = Exception::class)
fun testDoThing() {
whenever(mockAcraWrapper.logAndThrow(any())
.thenThrow(Exception())
doThing()
}
anything after the stub becomes unreachablekarelpeeters
03/29/2019, 10:07 PMlogAndThrow
call to do?matt tighe
03/29/2019, 10:08 PMkarelpeeters
03/29/2019, 10:09 PMlogAndThrow
, it always throws and .thenThrow()
and doThing()
never even happen, and that's what the compiler is warning about.matt tighe
03/29/2019, 10:10 PMlogAndThrow
shouldn’t be called until doThing()
is called and the subsequent call to thing()
failskarelpeeters
03/29/2019, 10:12 PMwhenever
do that causes the first line of testDoThing
to not simply call logAndThrow
?matt tighe
03/29/2019, 10:15 PMwhenever
is just a wrapper for mockito’s when
function, which is a keyword in kotlinmatt tighe
03/29/2019, 10:15 PMkarelpeeters
03/29/2019, 10:18 PMPavlo Liapota
03/29/2019, 10:23 PMwhenever
but in mockAcraWrapper
instance which is mocked, so it actually does not call original logAndThrow
method.
I didn’t work with mocking much, so I can’t help.
But do not “return garbage values”. For instance, you can call error()
function after acraWrapper.logAndThrow(err)
. This would be more explicit.Pavlo Liapota
03/29/2019, 10:26 PMfun doThing() {
try { thing() }
catch (err: Exception) {
acraWrapper.logAndThrow(err)
error("should not happen")
}
}
matt tighe
03/29/2019, 10:27 PM