abbic
02/03/2023, 12:21 PMinline fun <reified T: Throwable> assertThrows(clazz: KClass<T>, block: () -> Unit) {
var exception: Throwable? = null
try {
block()
} catch (e: Throwable) {
exception = e
} finally {
assertTrue(exception is T)
}
}
i used it expecting it to not work, but it successfully asserts T based on the clazz parameter i pass in, even though i dont actually use the clazz parameter in my function. is the runtime able to narrow down T based on the function parameter?abbic
02/03/2023, 12:25 PMassertThrows(NoSuchElementException::class) {
dao.getContractAccount(session)
}
assertThrows(NoSuchElementException::class) {
dao.getContractAccount(session)
}
assertThrows(IllegalStateException::class) {
dao.getContractAccount(session)
}
first 2 pass, last call failsRobert Williams
02/03/2023, 12:36 PMreified
doesRobert Williams
02/03/2023, 12:36 PMclazz
parameter and call it like
assertThrows<NoSuchElementException>() {
abbic
02/03/2023, 12:37 PMabbic
02/03/2023, 12:37 PMabbic
02/03/2023, 12:37 PMabbic
02/03/2023, 12:40 PMCLOVIS
02/03/2023, 1:19 PMassertFailsWith
or similarabbic
02/03/2023, 1:19 PMCLOVIS
02/03/2023, 1:20 PMabbic
02/03/2023, 1:21 PMabbic
02/03/2023, 1:21 PMCLOVIS
02/03/2023, 1:21 PMabbic
02/03/2023, 1:22 PMabbic
02/03/2023, 1:22 PMRobert Williams
02/03/2023, 1:42 PMRobert Williams
02/03/2023, 1:43 PMephemient
02/03/2023, 5:56 PM