Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support nullable Java Date/Time arguments in @ParameterizedTest methods #4010

Open
1 task
Clemens-L opened this issue Sep 19, 2024 · 2 comments · May be fixed by #4013
Open
1 task

Support nullable Java Date/Time arguments in @ParameterizedTest methods #4010

Clemens-L opened this issue Sep 19, 2024 · 2 comments · May be fixed by #4013

Comments

@Clemens-L
Copy link

When supplying a Java Time parameter in parameterized tests, it is not possible to null the parameter by supplying a null-String:

@Override
protected Object convert(Object input, Class<?> targetClass, JavaTimeConversionPattern annotation) {
if (input == null) {
throw new ArgumentConversionException("Cannot convert null to " + targetClass.getName());
}

In my tests, it would come in handy if I could just set a null string in my CsvSource and have the JavaTimeArgumentConverter null the Java Time parameter.

I would suggest making this behaviour configurable via the JavaTimeConversionPattern annotation, such as:

@ParameterizedTest
@CsvSource({", 01.01.1970", "01.01.1970, 01.01.1970"})
void test(
    @JavaTimeConversionPattern("dd.MM.yyyy", nullable = true) LocalDate nullableDate,
    @JavaTimeConversionPattern("dd.MM.yyyy") LocalDate nonNullableDate) {
    // ...
}

(with the old behaviour being the default).

Let me know what you think. If you deem this feature useful, I'd be happy to provide a PR for this.

Deliverables

  • Option to have JavaTimeArgumentConverter convert null-values
@marcphilipp
Copy link
Member

Thanks for raising this! I think your proposed solution makes sense. We should point to the new nullable attribute in the message of the ArgumentConversionException when it's set to false.

@marcphilipp marcphilipp added this to the 5.12 M1 milestone Sep 19, 2024
Clemens-L added a commit to Clemens-L/junit5 that referenced this issue Sep 19, 2024
Clemens-L added a commit to Clemens-L/junit5 that referenced this issue Sep 19, 2024
@Clemens-L Clemens-L linked a pull request Sep 19, 2024 that will close this issue
6 tasks
@Clemens-L
Copy link
Author

Hi @marcphilipp,

good to hear! I opened a PR and included your suggestion to mention the nullable option in the ArgumentConversionException. Let me know if I should change anything.

Thanks!

@sbrannen sbrannen linked a pull request Sep 19, 2024 that will close this issue
6 tasks
@sbrannen sbrannen changed the title Nullable Java Time parameters in parameterized tests Suppor nullable Java Date/Time arguments in @ParameterizedTest methods Sep 20, 2024
@sbrannen sbrannen changed the title Suppor nullable Java Date/Time arguments in @ParameterizedTest methods Support nullable Java Date/Time arguments in @ParameterizedTest methods Sep 20, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment