Search code examples
kotlinassertk

assertk test shouldn't pass (nested alls / any ?)


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 alls 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?

  • assertk version 0.23
  • junit version 5.7.0
  • kotlin version 1.3.72
  • java version 11.0.9.1

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


Solution

  • This was a bug in assertk: https://github.com/willowtreeapps/assertk/issues/331. The issue occurred when there was more than one failure in the any block.

    It's fixed in v0.23.1