SELF - the "self" type of this assertion class.VALUE - type of the value contained in the Optional.public abstract class AbstractOptionalAssert<SELF extends AbstractOptionalAssert<SELF,VALUE>,VALUE> extends AbstractAssert<SELF,Optional<VALUE>>
Optional.actual, info, myself, objects, throwUnsupportedExceptionOnEquals| Modifier | Constructor and Description |
|---|---|
protected |
AbstractOptionalAssert(Optional<VALUE> actual,
Class<?> selfType) |
| Modifier and Type | Method and Description |
|---|---|
SELF |
contains(VALUE expectedValue)
Verifies that the actual
Optional contains the given value (alias of hasValue(Object)). |
SELF |
containsInstanceOf(Class<?> clazz)
Verifies that the actual
Optional contains a value that is an instance of the argument. |
SELF |
containsSame(VALUE expectedValue)
Verifies that the actual
Optional contains the instance given as an argument (i.e. |
<U> AbstractOptionalAssert<?,U> |
flatMap(Function<? super VALUE,Optional<U>> mapper)
Call
flatMap on the Optional under test, assertions chained afterwards are performed on the Optional resulting from the flatMap call. |
AbstractObjectAssert<?,VALUE> |
get()
Verifies that the actual
Optional is not null and not empty and returns an Object assertion
that allows chaining (object) assertions on the optional value. |
<ASSERT extends AbstractAssert<?,?>> |
get(InstanceOfAssertFactory<?,ASSERT> assertFactory)
Verifies that the actual
Optional is not null and not empty and returns an new assertion instance
to chain assertions on the optional value. |
SELF |
hasValue(VALUE expectedValue)
Verifies that the actual
Optional contains the given value (alias of contains(Object)). |
SELF |
hasValueSatisfying(Condition<? super VALUE> condition)
|
SELF |
hasValueSatisfying(Consumer<VALUE> requirement)
|
SELF |
isEmpty()
Verifies that the actual
Optional is empty. |
SELF |
isNotEmpty()
Verifies that there is a value present in the actual
Optional, it's an alias of isPresent(). |
SELF |
isNotPresent()
|
SELF |
isPresent()
Verifies that there is a value present in the actual
Optional. |
<U> AbstractOptionalAssert<?,U> |
map(Function<? super VALUE,? extends U> mapper)
Call
map on the Optional under test, assertions chained afterwards are performed on the Optional resulting from the map call. |
SELF |
usingDefaultValueComparator()
Revert to standard comparison for incoming assertion
Optional value checks. |
SELF |
usingFieldByFieldValueComparator()
Deprecated.
This method is deprecated because it performs a shallow field by field comparison, i.e. elements are compared
field by field but the fields are compared with equals, use
get() chained with AbstractObjectAssert.usingRecursiveComparison() instead.
See https://assertj.github.io/doc/#assertj-core-recursive-comparison |
RecursiveComparisonAssert<?> |
usingRecursiveComparison()
Enable using a recursive field by field comparison strategy when calling the chained
RecursiveComparisonAssert, |
RecursiveComparisonAssert<?> |
usingRecursiveComparison(RecursiveComparisonConfiguration recursiveComparisonConfiguration)
Same as
usingRecursiveComparison() but allows to specify your own RecursiveComparisonConfiguration. |
SELF |
usingValueComparator(Comparator<? super VALUE> customComparator)
Use given custom comparator instead of relying on actual type A
equals method to compare the
Optional value's object for incoming assertion checks. |
areEqual, asInstanceOf, asList, assertionError, asString, describedAs, descriptionText, doesNotHave, doesNotHaveSameClassAs, doesNotHaveSameHashCodeAs, doesNotHaveToString, equals, extracting, extracting, failure, failureWithActualExpected, failWithActualExpectedAndMessage, failWithMessage, getWritableAssertionInfo, has, hashCode, hasSameClassAs, hasSameHashCodeAs, hasToString, inBinary, inHexadecimal, is, isElementOfCustomAssert, isEqualTo, isExactlyInstanceOf, isIn, isIn, isInstanceOf, isInstanceOfAny, isInstanceOfSatisfying, isNot, isNotEqualTo, isNotExactlyInstanceOf, isNotIn, isNotIn, isNotInstanceOf, isNotInstanceOfAny, isNotNull, isNotOfAnyClassIn, isNotSameAs, isNull, isOfAnyClassIn, isSameAs, matches, matches, newListAssertInstance, overridingErrorMessage, overridingErrorMessage, satisfies, satisfies, satisfies, satisfiesAnyOf, satisfiesAnyOf, satisfiesAnyOfForProxy, satisfiesForProxy, setCustomRepresentation, setDescriptionConsumer, setPrintAssertionsDescription, throwAssertionError, usingComparator, usingComparator, usingDefaultComparator, withFailMessage, withFailMessage, withRepresentation, withThreadDumpOnErrorclone, finalize, getClass, notify, notifyAll, toString, wait, wait, waitas, as, as, describedAspublic SELF isPresent()
Optional.
Assertion will pass :
assertThat(Optional.of("something")).isPresent();
Assertion will fail :
assertThat(Optional.empty()).isPresent();public SELF isNotEmpty()
Optional, it's an alias of isPresent().
Assertion will pass :
assertThat(Optional.of("something")).isNotEmpty();
Assertion will fail :
assertThat(Optional.empty()).isNotEmpty();public SELF isEmpty()
Optional is empty.
Assertion will pass :
assertThat(Optional.empty()).isEmpty();
Assertion will fail :
assertThat(Optional.of("something")).isEmpty();public SELF isNotPresent()
Optional is empty (alias of isEmpty()).
Assertion will pass :
assertThat(Optional.empty()).isNotPresent();
Assertion will fail :
assertThat(Optional.of("something")).isNotPresent();public SELF contains(VALUE expectedValue)
Optional contains the given value (alias of hasValue(Object)).
Assertion will pass :
assertThat(Optional.of("something")).contains("something");
assertThat(Optional.of(10)).contains(10);
Assertion will fail :
assertThat(Optional.of("something")).contains("something else");
assertThat(Optional.of(20)).contains(10);expectedValue - the expected value inside the Optional.public SELF hasValueSatisfying(Consumer<VALUE> requirement)
Optional contains a value and gives this value to the given
Consumer for further assertions. Should be used as a way of deeper asserting on the
containing object, as further requirement(s) for the value.
Assertions will pass :
// one requirement
assertThat(Optional.of(10)).hasValueSatisfying(i -> { assertThat(i).isGreaterThan(9); });
// multiple requirements
assertThat(Optional.of(someString)).hasValueSatisfying(s -> {
assertThat(s).isEqualTo("something");
assertThat(s).startsWith("some");
assertThat(s).endsWith("thing");
});
Assertions will fail :
assertThat(Optional.of("something")).hasValueSatisfying(s -> {
assertThat(s).isEqualTo("something else");
});
// fail because optional is empty, there is no value to perform assertion on
assertThat(Optional.empty()).hasValueSatisfying(o -> {});requirement - to further assert on the object contained inside the Optional.public SELF hasValueSatisfying(Condition<? super VALUE> condition)
Optional contains a value which satisfies the given Condition.
Examples:
Condition<TolkienCharacter> isAnElf = new Condition<>(character -> character.getRace() == ELF, "an elf");
TolkienCharacter legolas = new TolkienCharacter("Legolas", 1000, ELF);
TolkienCharacter frodo = new TolkienCharacter("Frodo", 33, HOBBIT);
// assertion succeeds
assertThat(Optional.of(legolas)).hasValueSatisfying(isAnElf);
// assertion fails
assertThat(Optional.of(frodo)).hasValueSatisfying(isAnElf);condition - the given condition.AssertionError - if the actual Optional is null or empty.NullPointerException - if the given condition is null.AssertionError - if the actual value does not satisfy the given condition.public SELF hasValue(VALUE expectedValue)
Optional contains the given value (alias of contains(Object)).
Assertion will pass :
assertThat(Optional.of("something")).hasValue("something");
assertThat(Optional.of(10)).contains(10);
Assertion will fail :
assertThat(Optional.of("something")).hasValue("something else");
assertThat(Optional.of(20)).contains(10);expectedValue - the expected value inside the Optional.public SELF containsInstanceOf(Class<?> clazz)
Optional contains a value that is an instance of the argument.
Assertions will pass:
assertThat(Optional.of("something")).containsInstanceOf(String.class)
.containsInstanceOf(Object.class);
assertThat(Optional.of(10)).containsInstanceOf(Integer.class);
Assertion will fail:
assertThat(Optional.of("something")).containsInstanceOf(Integer.class);clazz - the expected class of the value inside the Optional.@Deprecated public SELF usingFieldByFieldValueComparator()
get() chained with AbstractObjectAssert.usingRecursiveComparison() instead.
TolkienCharacter frodo = new TolkienCharacter("Frodo", 33, HOBBIT);
TolkienCharacter frodoClone = new TolkienCharacter("Frodo", 33, HOBBIT);
// frodo and frodoClone are equals when doing a field by field comparison.
assertThat(Optional.of(frodo)).get().usingRecursiveComparison()
.isEqualTo(frodoClone);
equals method to compare the Optional value's object for incoming assertion
checks. Private fields are included but this can be disabled using Assertions.setAllowExtractingPrivateFields(boolean).
This can be handy if equals method of the Optional value's object to compare does not suit
you.
equals method.
Example:
TolkienCharacter frodo = new TolkienCharacter("Frodo", 33, HOBBIT);
TolkienCharacter frodoClone = new TolkienCharacter("Frodo", 33, HOBBIT);
// Fail if equals has not been overridden in TolkienCharacter as equals default implementation only compares references
assertThat(Optional.of(frodo)).contains(frodoClone);
// frodo and frodoClone are equals when doing a field by field comparison.
assertThat(Optional.of(frodo)).usingFieldByFieldValueComparator().contains(frodoClone);this assertion object.public SELF usingValueComparator(Comparator<? super VALUE> customComparator)
equals method to compare the
Optional value's object for incoming assertion checks.
Custom comparator is bound to assertion instance, meaning that if a new assertion is created, it will use default comparison strategy.
Examples :
TolkienCharacter frodo = new TolkienCharacter("Frodo", 33, HOBBIT);
TolkienCharacter frodoClone = new TolkienCharacter("Frodo", 33, HOBBIT);
// Fail if equals has not been overridden in TolkienCharacter as equals default implementation only compares references
assertThat(Optional.of(frodo)).contains(frodoClone);
// frodo and frodoClone are equals when doing a field by field comparison.
assertThat(Optional.of(frodo)).usingValueComparator(new FieldByFieldComparator()).contains(frodoClone);customComparator - the comparator to use for incoming assertion checks.this assertion object.NullPointerException - if the given comparator is null.public SELF usingDefaultValueComparator()
Optional value checks.
This method should be used to disable a custom comparison strategy set by calling
usingValueComparator(Comparator).
this assertion object.public SELF containsSame(VALUE expectedValue)
Optional contains the instance given as an argument (i.e. it must be the
same instance).
Assertion will pass :
String someString = "something";
assertThat(Optional.of(someString)).containsSame(someString);
// Java will create the same 'Integer' instance when boxing small ints
assertThat(Optional.of(10)).containsSame(10);
Assertion will fail :
// not even equal:
assertThat(Optional.of("something")).containsSame("something else");
assertThat(Optional.of(20)).containsSame(10);
// equal but not the same:
assertThat(Optional.of(new String("something"))).containsSame(new String("something"));
assertThat(Optional.of(new Integer(10))).containsSame(new Integer(10));expectedValue - the expected value inside the Optional.public <U> AbstractOptionalAssert<?,U> flatMap(Function<? super VALUE,Optional<U>> mapper)
flatMap on the Optional under test, assertions chained afterwards are performed on the Optional resulting from the flatMap call.
Examples:
Function<String, Optional<String>> UPPER_CASE_OPTIONAL_STRING =
s -> s == null ? Optional.empty() : Optional.of(s.toUpperCase());
// assertions succeed
assertThat(Optional.of("something")).contains("something")
.flatMap(UPPER_CASE_OPTIONAL_STRING)
.contains("SOMETHING");
assertThat(Optional.<String>empty()).flatMap(UPPER_CASE_OPTIONAL_STRING)
.isEmpty();
assertThat(Optional.<String>ofNullable(null)).flatMap(UPPER_CASE_OPTIONAL_STRING)
.isEmpty();
// assertion fails
assertThat(Optional.of("something")).flatMap(UPPER_CASE_OPTIONAL_STRING)
.contains("something");U - the type wrapped in the Optional after the flatMap operation.mapper - the Function to use in the flatMap operation.AbstractOptionalAssert for assertions chaining on the flatMap of the Optional.AssertionError - if the actual Optional is null.public <U> AbstractOptionalAssert<?,U> map(Function<? super VALUE,? extends U> mapper)
map on the Optional under test, assertions chained afterwards are performed on the Optional resulting from the map call.
Examples:
// assertions succeed
assertThat(Optional.<String>empty()).map(String::length)
.isEmpty();
assertThat(Optional.of("42")).contains("42")
.map(String::length)
.contains(2);
// assertion fails
assertThat(Optional.of("42")).map(String::length)
.contains(3);U - the type wrapped in the Optional after the map operation.mapper - the Function to use in the map operation.AbstractOptionalAssert for assertions chaining on the map of the Optional.AssertionError - if the actual Optional is null.public AbstractObjectAssert<?,VALUE> get()
Optional is not null and not empty and returns an Object assertion
that allows chaining (object) assertions on the optional value.
Note that it is only possible to return Object assertions after calling this method due to java generics limitations.
Example:
TolkienCharacter frodo = new TolkienCharacter("Frodo", 33, HOBBIT);
TolkienCharacter sam = new TolkienCharacter("Sam", 38, null);
// assertion succeeds since all frodo's fields are set
assertThat(Optional.of(frodo)).get().hasNoNullFieldsOrProperties();
// assertion does not succeed because sam does not have its race set
assertThat(Optional.of(sam)).get().hasNoNullFieldsOrProperties();AbstractObjectAssert for assertions chaining on the value of the Optional.AssertionError - if the actual Optional is null.AssertionError - if the actual Optional is empty.get(InstanceOfAssertFactory)public <ASSERT extends AbstractAssert<?,?>> ASSERT get(InstanceOfAssertFactory<?,ASSERT> assertFactory)
Optional is not null and not empty and returns an new assertion instance
to chain assertions on the optional value.
The assertFactory parameter allows to specify an InstanceOfAssertFactory, which is used to get the
assertions narrowed to the factory type.
Wrapping the given InstanceOfAssertFactory with Assertions.as(InstanceOfAssertFactory) makes the
assertion more readable.
Example:
// assertion succeeds
assertThat(Optional.of("frodo")).get(as(InstanceOfAssertFactories.STRING)).startsWith("fro");
// assertion does not succeed because frodo is not an Integer
assertThat(Optional.of("frodo")).get(as(InstanceOfAssertFactories.INTEGER)).isZero();ASSERT - the type of the resulting AssertassertFactory - the factory which verifies the type and creates the new AssertAssert instance for assertions chaining on the value of the OptionalNullPointerException - if the given factory is nullAssertionError - if the actual Optional is nullAssertionError - if the actual Optional is emptypublic RecursiveComparisonAssert<?> usingRecursiveComparison()
RecursiveComparisonAssert,
Example:
public class Person {
String name;
boolean hasPhd;
}
public class Doctor {
String name;
boolean hasPhd;
}
Doctor drSheldon = new Doctor("Sheldon Cooper", true);
Person sheldon = new Person("Sheldon Cooper", true);
Optional<Doctor> doctor = Optional.of(drSheldon);
Optional<Person> person = Optional.of(sheldon);
// assertion succeeds as both maps contains equivalent items.
assertThat(doctor).usingRecursiveComparison()
.isEqualTo(person);
// assertion fails because leonard names are different.
drSheldon.setName("Sheldon Kooper");
assertThat(doctor).usingRecursiveComparison()
.isEqualTo(person);
A detailed documentation for the recursive comparison is available here: https://assertj.github.io/doc/#assertj-core-recursive-comparison.
The default recursive comparison behavior is configured as follows:
withStrictTypeChecking.java.lang.Double: DoubleComparator with precision of 1.0E-15java.lang.Float: FloatComparator with precision of 1.0E-6AbstractIterableAssert.usingComparatorForType(Comparator, Class) usingRecursiveComparison in class AbstractAssert<SELF extends AbstractOptionalAssert<SELF,VALUE>,Optional<VALUE>>RecursiveComparisonAssert instanceRecursiveComparisonConfigurationpublic RecursiveComparisonAssert<?> usingRecursiveComparison(RecursiveComparisonConfiguration recursiveComparisonConfiguration)
usingRecursiveComparison() but allows to specify your own RecursiveComparisonConfiguration.usingRecursiveComparison in class AbstractAssert<SELF extends AbstractOptionalAssert<SELF,VALUE>,Optional<VALUE>>recursiveComparisonConfiguration - the RecursiveComparisonConfiguration used in the chained isEqualTo assertion.RecursiveComparisonAssert instance built with the given RecursiveComparisonConfiguration.Copyright © 2025. All rights reserved.