Skip to content

single() assertion causes test that should fail to pass instead (example inside) #243

@MartinHaeusler

Description

@MartinHaeusler

I've witnessed a very strange behavior of strikt 0.29.0 (as well as 0.30.0) today. Please consider the following self-contained test:

import org.junit.jupiter.api.Test
import strikt.api.expectThat
import strikt.assertions.hasSize
import strikt.assertions.single

class StriktTest {

    @Test
    fun thisWillFailAsIntended(){
        val list = listOf("hello", "foo")

        expectThat(list).hasSize(3).and { // fails the "hasSize" condition, as it should.
            get { this.filter { it == "foo" } }.single()
        }
    }

    @Test
    fun thisWillActuallyPass() {
        val list = listOf("hello", "foo")

        expectThat(list) {
            hasSize(3) // we have 2 items in the list, yet the test passes
            get { this.filter { it == "foo" } }.single()
        }
    }

    @Test
    fun thisWillActuallyPassToo() {
        val list = listOf("hello", "foo")

        expectThat(list) {
            get { this.filter { it == "foo" } }.single()
            get { this.filter { it == "bar" } }.single() // "bar" is not even in the list - yet the test passes!
        }
    }

}

To my amazement, the second and third test will actually pass. Even though the list certainly doesn't match all of the given criteria.

I can't explain why that's the case. Did I do something horribly wrong with the API? Did I mis-interpret some of the methods?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions