Using Kotlin.Test to check that a function fails and throws an exception of type IOException is easy:
assertFailsWith<IOException> { myFun() }
However, i want to also check this call does NOT throw an exception of type E : IOException, where E is a subtype of IOException. Something like
assertFailsButWithout<E> { myFun() }
How do i do this? Searching for assertFailsWith here on SO only yields 5 unrelated results.
But why?
I'm testing my network stack implemented in retrofit okhttp.
The stack throws custom errors of type E - which have to subtype IOException, due to some weird restriction of retrofit. I now want to test a case like "A disconnect should fail with an IOException, NOT with one of my custom exceptions (e.g. a "MyEmptyResponseError")
CodePudding user response:
Test that it fails with IOException, and then assert that the exception is not your custom exception:
val thrown = assertFailsWith<IOException> { myFun() }
assertIsNot<MyEmptyResponseError>(thrown)
CodePudding user response:
Something like this :
assertFailsButWithout<E> {
try{
myFun()
assertTrue(true)
}
catch(ex :E){
assertTrue(false)
}
catch(ex :Exception){
assertTrue(true)
}
}
