Search code examples
kotlinjqwik

Constraining List values in jqwik with Kotlin


I'm writing jqwik tests in Kotlin. I have this parameter to one of my property tests:

@ForAll @Size(3) @UniqueElements emails: List<@Email @NotBlank String>

But when I run the test, it invariably produces

  emails: ["", "�", "A"]

which is clearly not following the constraint I thought I was giving. What am I doing wrong?


Solution

  • I just tried successfully:

    @Property(tries = 100)
    fun `stack overflow 77519262`(@ForAll @Size(3) @UniqueElements emails: List<@Email @NotBlank String>) {
        assert(emails.size == 3)
        assert(emails.toSet().size == 3)
        assert(emails.all { it.isNotBlank() })
        assert(emails.all { it.contains("@") })
    }
    

    I assume there's a misconfiguration on your side. Here's a few things that could go wrong:

    • Are you using the latest jqwik version, which is 1.8.1? Tested with Kotlin 1.8.0.
    • Have you added the jqwik-kotlin module as a dependency? This is necessary to mitigate some of the Java-Kotlin incompatibilities.
    • Have a look at the Kotlin chapter in the user guide to see some of the recommended configurations for Kotlin, e.g. "-Xemit-jvm-typ-annotations", which could be the culprit in your case.