Skip to content

Introduce @BooleansSource for use with @ParameterizedTest #4507

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

Closed
overwave opened this issue May 1, 2025 · 6 comments
Closed

Introduce @BooleansSource for use with @ParameterizedTest #4507

overwave opened this issue May 1, 2025 · 6 comments

Comments

@overwave
Copy link

overwave commented May 1, 2025

Our team extensively utilizes JUnit Jupiter, particularly the @ParameterizedTest feature, to streamline our testing processes.

A common pattern we have observed involves the use of the @ValueSource annotation with the booleans attribute. Typically, this attribute is used to check whether our code behaves consistently when a feature flag is enabled versus when it is not. Consequently, the majority of our tests require a boolean array containing both true and false values.

To address this need, we have developed several custom annotations that utilize @ValueSource(booleans = {true, false}). However, we believe this pattern is prevalent enough to warrant inclusion in the standard library, reducing redundancy and simplifying test code across projects.

Would it be possible to consider adding a default boolean array {true, false} option for the @ValueSource annotation in future JUnit releases? We believe this addition would benefit many users by eliminating the need to create custom annotations for such a common use case.

I can implement the feature myself, it it would be any help to this.

Thank you for your consideration of this proposal. I look forward to any feedback or questions you might have.

Rodion.

@sbrannen sbrannen changed the title Boolean source shortcut for @ValueSource Introduce @BooleansSource for use with @ParameterizedTest May 1, 2025
@sbrannen
Copy link
Member

sbrannen commented May 1, 2025

Hi @overwave,

Thanks for the proposal, and congratulations on submitting your first issue for JUnit 5. 👍

Would it be possible to consider adding a default boolean array {true, false} option for the @ValueSource annotation in future JUnit releases?

No, that would not be possible. All of the attributes in @ValueSource are optional (i.e., empty arrays by default), so that developers can choose which attribute the wish to use.

As stated in the Javadoc:

Note, however, that only one of the supported types may be specified per @ValueSource declaration.

Providing a default value other than an empty array would therefore break all other use cases.

However, it would be possible to introduce a dedicated argument source that always provides boolean true and false. That would be somewhat analogous to the @NullAndEmptySource support I introduced in JUnit Jupiter 5.7.

I have therefore changed the title of this issue to reflect that, and we will discuss the proposal within the team.

Note, however, that @BooleansSource is merely intended as a possible name for such an annotation.

Regards,

Sam

@sbrannen
Copy link
Member

sbrannen commented May 1, 2025

Such a boolean source can be implemented as a composed annotation as follows.

@Target({ ElementType.ANNOTATION_TYPE, ElementType.METHOD })
@Retention(RetentionPolicy.RUNTIME)
@Documented
@ValueSource(booleans = {true, false})
public @interface BooleansSource {
}

And that can be used as follows.

@ParameterizedTest
@BooleansSource
void test(boolean flag) {
	assertTrue(flag || !flag);
}

@overwave
Copy link
Author

overwave commented May 1, 2025

Hi, @sbrannen! Thanks for the reply.

Yes, I understand the solution requires a new annotation, as both you and me proposed above.

The request is to add such annotation (as you've mentioned) to the JUnit library so the code for booleans could be more concise.

The JUnit also has this verbose alternative: https://github.com/search?q=repo%3Ajunit-team%2Fjunit5%20%40ValueSource(booleans%20%3D%20%7B%20true%2C%20false%20%7D)&type=code

I will understand if you decide to have more clean API rather than many shortcuts, but this could make our code (and I think someone's too) a little bit tidier.

Best regards,
Rodion

@overwave
Copy link
Author

overwave commented May 8, 2025

Hi @sbrannen, @marcphilipp and the rest of JUnit Team,

I hope this message finds you well.

I am still very enthusiastic about contributing to this enhancement and am eager to implement the feature if it aligns with your future plans for JUnit. Please let me know if this proposal is under consideration or if there is any additional information or assistance I can provide to help move this forward.

Thank you for your attention to this matter. I appreciate the work you do to make JUnit a robust and user-friendly testing framework, and I hope to contribute to this effort.

Looking forward to your feedback.

Best regards,
Rodion

@marcphilipp marcphilipp linked a pull request May 8, 2025 that will close this issue
6 tasks
@marcphilipp
Copy link
Member

@overwave I like your enthusiasm! 🙂

However, as stated in #4518 (comment), we first need to discuss whether we want to add this feature in the team before reviewing a potential solution. We'll post here when that has happened.

@marcphilipp
Copy link
Member

Team decision: Since @ValueSource(booleans = {true, false}) already exists and provides more flexibility (in terms of order and number of arguments), we don't think introducing a dedicated annotation for this purpose would be beneficial.

@marcphilipp marcphilipp closed this as not planned Won't fix, can't repro, duplicate, stale May 23, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants