jqwik-team / jqwik

Property-Based Testing on the JUnit Platform
http://jqwik.net
Eclipse Public License 2.0
575 stars 64 forks source link

Data-Driven tests cannot provide null values for parameters #308

Closed rujames closed 2 years ago

rujames commented 2 years ago

Testing Problem

Data-driven tests for which tuples contain null values cannot be executed, as the DataBasedShrinkablesGenerator is unable to retrieve a class when checking for compatibility.

    @Data
    fun fizzBuzzExamples() = listOf(
            Tuple.of(null, null),
            Tuple.of(1, "1"),
            Tuple.of(3, "Fizz"),
            Tuple.of(5, "Buzz"),
            Tuple.of(15, "FizzBuzz")
        )

    @Property
    @FromData("fizzBuzzExamples")
    fun fizzBuzzWorks(@ForAll index: Int?, @ForAll result: String?) {
        fizzBuzz(index) shouldBe result
    }
java.lang.NullPointerException
    at net.jqwik.engine.properties.DataBasedShrinkablesGenerator.checkCompatibility(DataBasedShrinkablesGenerator.java:45)

This can be sidestepped with the use of optionals, but this seems like unnecessary cruft and can hurt the readability of both the data and the property test.

Suggested Solution

DataBasedShrinkablesGenerator should handle the case of a null tuple entry without throwing a Null Pointer Exception - instead of determining whether the valueType (which isn't defined) can be assigned to the parameterType, it could instead verify that the parameterType accepts null values. Perhaps the TypeUsage isn't even required for the value, and all values can be directly validated against the parameterType.

I'm not sure how such a solution would be realised, but I'm happy to have a deeper look.

jlink commented 2 years ago

Thanks for the catch. Looks like a bug at first glance. Will verify when back at work, probably next week.

jlink commented 2 years ago

@rujames BTW, Tuple.of(1, 1) does not fit the type of fizzBuzzWorks method.

jlink commented 2 years ago

Fixed in https://github.com/jlink/jqwik/commit/524a1fe178de8136a6ee525854733596b5a24168

jlink commented 2 years ago

Available in 1.6.5-SNAPSHOT.