Reputation: 2834
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?
Upvotes: 3
Views: 1113
Reputation: 14660
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.
Upvotes: 4
Reputation: 25573
You're using kotlin.assert
which are only enabled when platform assertions are enabled. The checks that you say should cause errors simply are never executed.
Either run it with the -ea
(enable assertsions) JVM parameter or use assertEquals
which is the usual test framework name since assert
is a keyword in java.
Upvotes: 3
Reputation: 120
Looking for inside documentation, assertEquals hasn't two String parameters to compare. It only receive generic Objects:
assertEquals(java.lang.Object expected, java.lang.Object actual) => Asserts that two objects are equal.
Strings should be compared using .equals(String)
method due using Objects have differents address memory access, so these are differents despite of they have same content.
:: Edit ::
To have the sames Objects type of Strings you should use .clone()
method. It returns same content and same references from original object.
Upvotes: -3