8000 feat(optional assertion): add hasValueMatching method to verify predicate matches by belljun3395 · Pull Request #3863 · asser 8000 tj/assertj · GitHub
[go: up one dir, main page]

Skip to content

feat(optional assertion): add hasValueMatching method to verify predicate matches#3863

Closed
belljun3395 wants to merge 11 commits intoassertj:mainfrom
belljun3395:add-hasValueMatching-method
Closed

feat(optional assertion): add hasValueMatching method to verify predicate matches#3863
belljun3395 wants to merge 11 commits intoassertj:mainfrom
belljun3395:add-hasValueMatching-method

Conversation

@belljun3395
Copy link
Contributor

Overview

This PR resolves issue #3848 by introducing a hasValueMatching(Predicate<T>) method to OptionalAssert. This new assertion provides a more fluent and direct way to verify that the value contained within an Optional satisfies a given predicate, enhancing the expressiveness of tests involving Optional types.

The Motivation

Currently, while ObjectAssert offers a convenient matches(Predicate) method, OptionalAssert lacks an equivalent, direct mechanism. To test a predicate against the value of an Optional, a user has to resort to less intuitive or unsafe patterns:

// This can throw a NoSuchElementException if the optional is empty.
assertThat(optional.get()).matches(MyPredicates::isAdult);

// This is safer, but more verbose than necessary.
assertThat(optional).hasValueSatisfying(value -> assertThat(value).matches(MyPredicates::isAdult));

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 to AbstractOptionalAssert, making it available to all Optional assertion classes.

The new assertion is used as follows:

Optional<Person> optional = getPerson();

// The new, more fluent assertion:
// It checks for presence and that the value matches the predicate.
assertThat(optional).hasValueMatching(Person::isAdult);

// An overloaded version with a custom description for clearer failure messages.
assertThat(optional).hasValueMatching(Person::isAdult, "is an adult");

This implementation first safely checks if a value is present in the Optional.

  • If the Optional is empty, it fails with an error indicating a value was expected.
  • If the value is present, it is evaluated against the Predicate. If the predicate test fails, an AssertionError is thrown with a descriptive message, using the existing ShouldMatch error 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:

@belljun3395 belljun3395 changed the title feat(optional assertion): add hasValueMatching method to verify predi… feat(optional assertion): add hasValueMatching method to verify predicate matches Aug 7, 2025
@joel-costigliola joel-costigliola self-requested a review August 13, 2025 14:17
@belljun3395
Copy link
Contributor Author

@joel-costigliola I have resolved the issue introduced in this commit 😃

@joel-costigliola
Copy link
Member

Thanks !

@joel-costigliola
Copy link
Member

Integrated with minor edits, thanks @belljun3395 !

@joel-costigliola joel-costigliola added this to the 4.0.0-M2 milestone Aug 13, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add OptionalAssert.hasValueMatching(Predicate<T>)

2 participants

0