I have a test that should pass but doesn't, and in the process of coming up with a small test case demonstrating the behaviour, I made a test that shouldn't pass but does. This is the test:
@Test
fun `should not pass`() {
val key1 = "key1"
val value1 = "value1"
val key2 = "key2"
val value2 = "value2"
val map: Map<String, Collection<Any>> = mapOf(key1 to listOf(value1), key2 to setOf(value2))
assertThat(map.entries).all {
any {
it.all {
prop(Map.Entry<String, Any>::key).isEqualTo(key1) // assert 1
prop(Map.Entry<Any, Collection<Any>>::key).isEqualTo(value1) // assert 2
fail("Expected value", "Actual value") // assert 3
}
}
}
}
I made a typo with assert 2
and extracted the key instead of the value. When I realised that, I couldn't figure out why the test still passed. So I added assert 3
to force it to fail, but the test still passed! Curiously, if I remove the second entry (key2
and value2
) from the map, then the test does fail as expected.
I suspect it might be something to do with the nested all
s and perhaps the any
, but I have a reason for setting it up like that. I have other assertions to make that have been omitted in the interests of keeping the example small.
Does anyone have a clue as to why this test is passing when it shouldn't?
Gradle dependencies:
implementation(platform("org.jetbrains.kotlin:kotlin-bom"))
implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8")
testImplementation("org.jetbrains.kotlin:kotlin-test")
testImplementation("org.jetbrains.kotlin:kotlin-test-junit5")
testImplementation("com.willowtreeapps.assertk:assertk-jvm:0.23")
testImplementation("io.mockk:mockk:1.10.3-jdk8")
testRuntimeOnly("org.junit.jupiter:junit-jupiter-engine:5.7.0")
Test class with imports:
import assertk.all
import assertk.assertThat
import assertk.assertions.any
import assertk.assertions.isEqualTo
import assertk.assertions.prop
import kotlin.test.Test
class AssertkNestedAllSimpleTest {
@Test
fun `should not pass`() {
val key1 = "key1"
val value1 = "value1"
val key2 = "key2"
val value2 = "value2"
val map: Map<String, Collection<Any>> = mapOf(key1 to listOf(value1), key2 to setOf(value2))
assertThat(map.entries).all {
any {
it.all {
prop(Map.Entry<String, Any>::key).isEqualTo(key1) // assert 1
prop(Map.Entry<Any, Collection<Any>>::key).isEqualTo(value1) // assert 2
}
}
}
}
}
GitHub project with full code for example: https://github.com/bschelberg/assertk-test-issue
this test fails. check your assertThat and @Test imports and check if it still passed with these imports :
import assertk.all
import assertk.assertThat
import assertk.assertions.*
import assertk.fail
import org.junit.Test
and this maven dependency the test fails as it should.
<dependency>
<groupId>com.willowtreeapps.assertk</groupId>
<artifactId>assertk-jvm</artifactId>
<version>0.23</version>
<scope>test</scope>
</dependency>
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