I'm working with OptionalAssert
class of AssertJ and I need to implement a JUnit ParameterizedTest
that will check for presence or emptiness of an Optional instance in a dynamic way:
@ParameterizedTest
@MethodSource(/* values */)
void test_presence(Optional<String> opt, boolean empty) {
assertThat(opt) // -> .isPresent() / .isEmpty();
}
In a non-parametrised test I would use .isPresent()
or .isEmpty()
methods to execute the check, but in this case I'd like to apply something like .isPresent(true/false)
.
I can't find a method like this in the JavaDoc so I'm wondering if there is an alternative approach to this (or should I just deal with an if/else
?)
UPDATE I know that I could implement something like so (as suggested in an answer):
assertThat(opt.isPresent()).isEqualTo(present);
but I'd like to maintain a fluent approach, and code similar to this:
assertThat(opt)
.isPresent(present) // true/false
.hasValueSatisfying(...)
.hasValueSatisfying(...)
// etc.