Add assertThrows variants which accept exceptions as values#5819
Add assertThrows variants which accept exceptions as values#5819mdedetrich wants to merge 1 commit into
Conversation
9c94c33 to
e44a582
Compare
e44a582 to
d2c0d8e
Compare
|
@mdedetrich Can't you use |
d2c0d8e to
95ba98b
Compare
Not with a kotlin suspend function that you are asserting, thats the precise issue. It will fail to compile for the reasons I stated in the PR. Kotlin suspend only works if the Of course you could just do Assertions.assertThrows(IllegalArgumentException::class.java) {
runBlocking {
someKotlinSuspendFunc()
}
}But one of the whole point of I have just rebased the PR against latest main to resolve conflicts. |
Sorry for making you re-explain it. I didn't read carefully enough. 😬 |
This comment has been minimized.
This comment has been minimized.
Signed-off-by: Matthew de Detrich <matthew.dedetrich@gls-itservices.com>
95ba98b to
d3494e9
Compare
Currently there exists
org.junit.jupiter.api.assertThrowsand its various variants to be used with JUnit and Kotlin that are also compatible with coroutines (i.e.suspend func). The current design uses reified inline functions, which generally work aside from one situation, specifically@ParameterizedTestthat produces exceptions classes as values and you intend to useassertThrowson those exception values.In this case, the current
org.junit.jupiter.api.assertThrowsdoesn't work because in order to use it you would have to make your test function (the one annotated with@ParameterizedTest) aninline funcwith a reifiedT: Throwablehowever this won't be picked up by JUnit as@ParameterizedTestuses reflection.This PR adds variants of the currently existing
org.junit.jupiter.api.assertThrowsthat have anexpectedType: Class<T>as the first parameter (which is the same convention asorg.junit.jupiter.api.Assertions.assertThrows) but since these added variants areinline fun, functions/methods used in the body can besuspend func(this is verified with the added tests).The current implementation is a bit of boilerplate however this is inevitable to a degree since we cannot use the currently existing
org.junit.jupiter.api.assertThrowsand its variants implementations as they only accept reifiedT: Throwablewhich then defeats the purpose of what we are trying to achieve.I hereby agree to the terms of the JUnit Contributor License Agreement.
Definition of Done
@APIannotations