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

Closed
1 task
Clemens-L opened this issue Sep 19, 2024 · 3 comments
Closed
1 task

Comments

@Clemens-L
Copy link
Contributor

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.

@Clemens-L
Copy link
Contributor 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
@sbrannen
Copy link
Member

@Clemens-L, congratulations on submitting your first issue and your first PR for JUnit 5! 👍

Your PR has been merged into main in e8c92f8.

Thanks

@sbrannen sbrannen self-assigned this Sep 23, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment