feat(optional assertion): add hasValueMatching method to verify predicate matches#3863
Closed
belljun3395 wants to merge 11 commits intoassertj:mainfrom
Closed
feat(optional assertion): add hasValueMatching method to verify predicate matches#3863belljun3395 wants to merge 11 commits intoassertj:mainfrom
belljun3395 wants to merge 11 commits intoassertj:mainfrom
Conversation
…cate matches Signed-off-by: belljun3395 <195850@jnu.ac.kr>
Signed-off-by: belljun3395 <195850@jnu.ac.kr>
Contributor
Author
|
@joel-costigliola I have resolved the issue introduced in this commit 😃 |
Member
|
Thanks ! |
Member
|
Integrated with minor edits, thanks @belljun3395 ! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Overview
This PR resolves issue #3848 by introducing a
hasValueMatching(Predicate<T>)method toOptionalAssert. This new assertion provides a more fluent and direct way to verify that the value contained within anOptionalsatisfies a given predicate, enhancing the expressiveness of tests involvingOptionaltypes.The Motivation
Currently, while
ObjectAssertoffers a convenientmatches(Predicate)method,OptionalAssertlacks an equivalent, direct mechanism. To test a predicate against the value of anOptional, a user has to resort to less intuitive or unsafe patterns:The absence of a direct method makes the test code either less readable or less robust. This PR aims to provide a dedicated assertion that is both safe and expressive.
The Solution
To address this,
hasValueMatching(Predicate)and an overloaded version with a custom description have been added toAbstractOptionalAssert, making it available to allOptionalassertion classes.The new assertion is used as follows:
This implementation first safely checks if a value is present in the
Optional.Optionalis empty, it fails with an error indicating a value was expected.Predicate. If the predicate test fails, anAssertionErroris thrown with a descriptive message, using the existingShouldMatcherror factory to ensure consistency with other assertions in AssertJ.This approach provides a clean, safe, and readable API for a common testing scenario.
Check List:
OptionalAssert.hasValueMatching(Predicate<T>)#3848