google / TestParameterInjector

A simple yet powerful parameterized test runner for Java.
Apache License 2.0
397 stars 34 forks source link

Support for null strings #4

Closed shafty023 closed 3 years ago

shafty023 commented 3 years ago

Any tips/suggestions for getting null as a value? I know JSR won't allow null to be passed in because it's not a constant but is there any clean way of doing it besides generating my own TestParameterValuesProvider for every parameter that needs to support null?

// Doesn't work
@Test
fun someTest(@TestParameter(value = [null, "", "foo"]) param: String) {
    ...
}

// Ugly bc you'd have to define the provider for every String argument in every test method
@Test
fun someTest(@TestParameter(valuesProvider = FooProvider::class) param: String) {
    ...
}

class FooProvider : TestParameter.TestParameterValuesProvider {
    override fun provideValues(): MutableList<String?> {
        return mutableListOf(null, "", "foo")
    }
}
nymanjens commented 3 years ago

Hi Daniel,

At the moment, your workaround seems to be the best solution for getting a null value, but I agree it's not ideal.

The following change would make it a lot easier to test for null values:

@Test
fun someTest(@TestParameter(value = ["null", "", "foo"]) param: String) {
   // "null" wouldn't be interpreted as String but as null value
    ...
}

I've implemented this and I'm now re-running all Google tests to see if any existing code is relying on a "null" string being "null". If this turns out to work, I'll push it onto a new release.

nymanjens commented 3 years ago

This change has been released in v1.3

shafty023 commented 3 years ago

@nymanjens Thanks! This is exactly what I needed. You rock.