I've created sample class to test assertions in Kotlin
class Assertion {
fun sum(a: Int, b: Int): Int {
assert(a > 0 && b > 0)
return a + b
}
}
fun main(args: Array<String>) {
Assertion().sum(-1, 2)
}
and was using the following options but the program doesn't throw assert exception.
-ea:AssertionKt
, -ea:Assertion
and -ea:...
To configure assertion options one must use either the -ea or -da command line flags to enable or disable assertions with the command line tool: “java”. For example, “java -ea Assert” where Assert is a java class file. You may also specify a specific class or package as follows.
assert(Boolean) throws AssertionError when its argument is false (but only if JVM assertions are enabled with -ea ). Use it to clarify outcomes and check your work.
An assertion is made using the assert keyword. Its syntax is: assert condition; Here, condition is a boolean expression that we assume to be true when the program executes.
To enable assertions in Kotlin, run the JVM with the -ea
option, without any additional specifiers.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With