Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unexpected assert behavior

I'm new to Kotlin Android so in writing tests these asserts unexpectedly pass:

import org.junit.Test

assert("x".equals("y"))
assert("x" == "y")

but this does fail:

import junit.framework.Assert.assertEquals
assertEquals("x", "y")

So I reviewed string structural comparisons.

And then found that this also passes:

assert(false)

Looks like org.junit.Test comes by default in a new project via:

testImplementation 'junit:junit:4.12'

So now I'm wondering what's the correct testImplementation package to sync with. Am I using the wrong one?

like image 636
jchristof Avatar asked Feb 21 '26 19:02

jchristof


1 Answers

From the doc for assert (https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/assert.html):

Throws an AssertionError if the value is false and runtime assertions have been enabled on the JVM using the -ea JVM option.

What you should do is to use Assert.assertTrue or Assert.assertEquals from org.junit instead.

like image 136
Anatolii Avatar answered Feb 23 '26 07:02

Anatolii



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!