Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

kotlin

assertk

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

like image 462
Bernie Avatar asked Nov 07 '22 03:11

Bernie


1 Answers

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>
like image 107
Naor Tedgi Avatar answered Nov 22 '22 06:11

Naor Tedgi