-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Implicit boolean conversion in parameterized tests should be strict and accept only true
or false
#3177
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
Comments
Hi @s-rwe, Congratulations on raising your first issue for JUnit 5! 👍 I agree that we should explicitly check for So I'm slating this for 5.10. Are you interested in submitting a PR to address this? |
Sure, thanks - created PR #3178 accordingly |
true
or false
Cause use of anything other than `true` or `false` (case-insensitive) fail in conversion, to make potential typos explicitly visible (instead of implicitly just treating all else as `false`). Includes a few tests for failures on invalid values, for conversions handled by `StringToBooleanAndCharPrimitiveConverter`. Resolves #3177. Co-authored-by: Marc Philipp <mail@marcphilipp.de>
This was a BAD idea and breaks many existing tests on our side. The general rule is that it looks for a constructor or factory method which takes a string and calls that to create the object. For Boolean, this means that the string (case insensitive) 'true' creates a Boolean.TRUE and everything else Boolean.FALSE. We actually made use of this for creating karnaugh tables in CSV sources, which have three values: true, false and don't care. The don't care is added as a '?' or 'X' symbol and creates a Boolean.FALSE (which is ok, could also be Boolean.TRUE - because; we don't care). Real (but simple) Example:
If something is read-only, we don't care if the user has write access or not - it is not editable. Sure, we could visit all tests now and replace '?' or 'X' by 'false' but that immediately changes the semantics of the test. IMHO, this should never have been accepted. |
We encountered a glitch in parameterized tests, where tests can be unintentionally running with
false
values for an argument, for example if there is a typo in the arguments of a@CsvSource
. If such tests still happen to be successful, it's likely that no one will notice the mistake.The implicit conversion is just using
Boolean::valueOf
under the hood AFAICS (StringToBooleanAndCharPrimitiveConverter
), which is very lenient in terms of "bad" input, which all just gets mapped to false.Sample snippet to show problematic cases (which would have clearly been intended to be "true" values):
Since this can hide problems and prevent tests from running through as intended, it should be reasonable to make the implicit conversion just fail on "invalid" input, to show mistakes and force developers to correct them accordingly (i.e. ensure that only values
true
+false
are acceptable, with case-insensitive matching).This is also an example for which customizing the implicit argument converters could be helpful (#853 which was closed, I think that possibility doesn't exist). But IMO it should be fine to just adjust the default conversion in general, to avoid such accidental mistakes in general.
The text was updated successfully, but these errors were encountered: