Class AbstractOptionalAssert<SELF extends AbstractOptionalAssert<SELF,VALUE>,VALUE>
- java.lang.Object
-
- org.assertj.core.api.AbstractAssert<SELF,Optional<VALUE>>
-
- org.assertj.core.api.AbstractOptionalAssert<SELF,VALUE>
-
- Type Parameters:
SELF- the "self" type of this assertion class.VALUE- type of the value contained in theOptional.
- All Implemented Interfaces:
Assert<SELF,Optional<VALUE>>,Descriptable<SELF>,ExtensionPoints<SELF,Optional<VALUE>>
- Direct Known Subclasses:
OptionalAssert
public abstract class AbstractOptionalAssert<SELF extends AbstractOptionalAssert<SELF,VALUE>,VALUE> extends AbstractAssert<SELF,Optional<VALUE>>
Assertions forOptional.- Author:
- Jean-Christophe Gay, Nicolai Parlog, Grzegorz Piwowarek
-
-
Field Summary
Fields Modifier and Type Field Description private ComparisonStrategyoptionalValueComparisonStrategy-
Fields inherited from class org.assertj.core.api.AbstractAssert
actual, assertionErrorCreator, conditions, info, myself, objects, throwUnsupportedExceptionOnEquals
-
-
Constructor Summary
Constructors Modifier Constructor Description protectedAbstractOptionalAssert(Optional<VALUE> actual, Class<?> selfType)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description private voidassertValueIsPresent()private voidcheckNotNull(Object expectedValue)SELFcontains(VALUE expectedValue)Verifies that the actualOptionalcontains the given value (alias ofhasValue(Object)).SELFcontainsInstanceOf(Class<?> clazz)Verifies that the actualOptionalcontains a value that is an instance of the argument.SELFcontainsSame(VALUE expectedValue)Verifies that the actualOptionalcontains the instance given as an argument (i.e.<U> AbstractOptionalAssert<?,U>flatMap(Function<? super VALUE,Optional<U>> mapper)CallflatMapon theOptionalunder test, assertions chained afterwards are performed on theOptionalresulting from the flatMap call.AbstractObjectAssert<?,VALUE>get()Verifies that the actualOptionalis notnulland not empty and returns an Object assertion that allows chaining (object) assertions on the optional value.SELFhasValue(VALUE expectedValue)Verifies that the actualOptionalcontains the given value (alias ofcontains(Object)).SELFhasValueSatisfying(Consumer<VALUE> requirement)SELFhasValueSatisfying(Condition<? super VALUE> condition)SELFisEmpty()Verifies that the actualOptionalis empty.SELFisNotEmpty()Verifies that there is a value present in the actualOptional, it's an alias ofisPresent().SELFisNotPresent()SELFisPresent()Verifies that there is a value present in the actualOptional.<U> AbstractOptionalAssert<?,U>map(Function<? super VALUE,? extends U> mapper)Callmapon theOptionalunder test, assertions chained afterwards are performed on theOptionalresulting from the map call.SELFusingDefaultValueComparator()Revert to standard comparison for incoming assertionOptionalvalue checks.SELFusingFieldByFieldValueComparator()Use field/property by field/property comparison (including inherited fields/properties) instead of relying on actual type Aequalsmethod to compare theOptionalvalue's object for incoming assertion checks.SELFusingValueComparator(Comparator<? super VALUE> customComparator)Use given custom comparator instead of relying on actual type Aequalsmethod to compare theOptionalvalue's object for incoming assertion checks.-
Methods inherited from class org.assertj.core.api.AbstractAssert
as, as, asInstanceOf, asList, asString, describedAs, describedAs, descriptionText, doesNotHave, doesNotHaveSameClassAs, equals, failWithMessage, getWritableAssertionInfo, has, hashCode, hasSameClassAs, hasSameHashCodeAs, hasToString, inBinary, inHexadecimal, is, isEqualTo, isExactlyInstanceOf, isIn, isIn, isInstanceOf, isInstanceOfAny, isInstanceOfSatisfying, isNot, isNotEqualTo, isNotExactlyInstanceOf, isNotIn, isNotIn, isNotInstanceOf, isNotInstanceOfAny, isNotNull, isNotOfAnyClassIn, isNotSameAs, isNull, isOfAnyClassIn, isSameAs, matches, matches, newListAssertInstance, overridingErrorMessage, satisfies, satisfies, satisfiesAnyOf, satisfiesAnyOf, setCustomRepresentation, throwAssertionError, usingComparator, usingComparator, usingDefaultComparator, withAssertionState, withFailMessage, withRepresentation, withThreadDumpOnError
-
-
-
-
Field Detail
-
optionalValueComparisonStrategy
private ComparisonStrategy optionalValueComparisonStrategy
-
-
Method Detail
-
isPresent
public SELF isPresent()
Verifies that there is a value present in the actualOptional.Assertion will pass :
Assertion will fail :assertThat(Optional.of("something")).isPresent();assertThat(Optional.empty()).isPresent();- Returns:
- this assertion object.
-
isNotEmpty
public SELF isNotEmpty()
Verifies that there is a value present in the actualOptional, it's an alias ofisPresent().Assertion will pass :
Assertion will fail :assertThat(Optional.of("something")).isNotEmpty();assertThat(Optional.empty()).isNotEmpty();- Returns:
- this assertion object.
-
isEmpty
public SELF isEmpty()
Verifies that the actualOptionalis empty.Assertion will pass :
Assertion will fail :assertThat(Optional.empty()).isEmpty();assertThat(Optional.of("something")).isEmpty();- Returns:
- this assertion object.
-
isNotPresent
public SELF isNotPresent()
Verifies that the actualOptionalis empty (alias ofisEmpty()).Assertion will pass :
Assertion will fail :assertThat(Optional.empty()).isNotPresent();assertThat(Optional.of("something")).isNotPresent();- Returns:
- this assertion object.
-
contains
public SELF contains(VALUE expectedValue)
Verifies that the actualOptionalcontains the given value (alias ofhasValue(Object)).Assertion will pass :
Assertion will fail :assertThat(Optional.of("something")).contains("something"); assertThat(Optional.of(10)).contains(10);assertThat(Optional.of("something")).contains("something else"); assertThat(Optional.of(20)).contains(10);- Parameters:
expectedValue- the expected value inside theOptional.- Returns:
- this assertion object.
-
hasValueSatisfying
public SELF hasValueSatisfying(Consumer<VALUE> requirement)
Verifies that the actualOptionalcontains a value and gives this value to the givenConsumerfor 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 :
Assertions will fail :// 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"); });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 -> {});- Parameters:
requirement- to further assert on the object contained inside theOptional.- Returns:
- this assertion object.
-
hasValueSatisfying
public SELF hasValueSatisfying(Condition<? super VALUE> condition)
Verifies that the actualOptionalcontains a value which satisfies the givenCondition.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);- Parameters:
condition- the given condition.- Returns:
- this assertion object.
- Throws:
AssertionError- if the actualOptionalis null or empty.NullPointerException- if the given condition isnull.AssertionError- if the actual value does not satisfy the given condition.- Since:
- 3.6.0
-
hasValue
public SELF hasValue(VALUE expectedValue)
Verifies that the actualOptionalcontains the given value (alias ofcontains(Object)).Assertion will pass :
Assertion will fail :assertThat(Optional.of("something")).hasValue("something"); assertThat(Optional.of(10)).contains(10);assertThat(Optional.of("something")).hasValue("something else"); assertThat(Optional.of(20)).contains(10);- Parameters:
expectedValue- the expected value inside theOptional.- Returns:
- this assertion object.
-
containsInstanceOf
public SELF containsInstanceOf(Class<?> clazz)
Verifies that the actualOptionalcontains a value that is an instance of the argument.Assertions will pass:
Assertion will fail:assertThat(Optional.of("something")).containsInstanceOf(String.class) .containsInstanceOf(Object.class); assertThat(Optional.of(10)).containsInstanceOf(Integer.class);assertThat(Optional.of("something")).containsInstanceOf(Integer.class);- Parameters:
clazz- the expected class of the value inside theOptional.- Returns:
- this assertion object.
-
usingFieldByFieldValueComparator
public SELF usingFieldByFieldValueComparator()
Use field/property by field/property comparison (including inherited fields/properties) instead of relying on actual type Aequalsmethod to compare theOptionalvalue's object for incoming assertion checks. Private fields are included but this can be disabled usingAssertions.setAllowExtractingPrivateFields(boolean).This can be handy if
Note that the comparison is not recursive, if one of the fields/properties is an Object, it will be compared to the other field/property using itsequalsmethod of theOptionalvalue's object to compare does not suit you.equalsmethod.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);- Returns:
thisassertion object.
-
usingValueComparator
public SELF usingValueComparator(Comparator<? super VALUE> customComparator)
Use given custom comparator instead of relying on actual type Aequalsmethod to compare theOptionalvalue'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);- Parameters:
customComparator- the comparator to use for incoming assertion checks.- Returns:
thisassertion object.- Throws:
NullPointerException- if the given comparator isnull.
-
usingDefaultValueComparator
public SELF usingDefaultValueComparator()
Revert to standard comparison for incoming assertionOptionalvalue checks.This method should be used to disable a custom comparison strategy set by calling
usingValueComparator(Comparator).- Returns:
thisassertion object.
-
containsSame
public SELF containsSame(VALUE expectedValue)
Verifies that the actualOptionalcontains the instance given as an argument (i.e. it must be the same instance).Assertion will pass :
Assertion will fail :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);// 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));- Parameters:
expectedValue- the expected value inside theOptional.- Returns:
- this assertion object.
-
flatMap
public <U> AbstractOptionalAssert<?,U> flatMap(Function<? super VALUE,Optional<U>> mapper)
CallflatMapon theOptionalunder test, assertions chained afterwards are performed on theOptionalresulting 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");- Type Parameters:
U- the type wrapped in theOptionalafter theflatMapoperation.- Parameters:
mapper- theFunctionto use in theflatMapoperation.- Returns:
- a new
AbstractOptionalAssertfor assertions chaining on the flatMap of the Optional. - Throws:
AssertionError- if the actualOptionalis null.- Since:
- 3.6.0
-
map
public <U> AbstractOptionalAssert<?,U> map(Function<? super VALUE,? extends U> mapper)
Callmapon theOptionalunder test, assertions chained afterwards are performed on theOptionalresulting 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);- Type Parameters:
U- the type wrapped in theOptionalafter themapoperation.- Parameters:
mapper- theFunctionto use in themapoperation.- Returns:
- a new
AbstractOptionalAssertfor assertions chaining on the map of the Optional. - Throws:
AssertionError- if the actualOptionalis null.- Since:
- 3.6.0
-
get
public AbstractObjectAssert<?,VALUE> get()
Verifies that the actualOptionalis notnulland 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().hasNoNullFields(); // assertion does not succeed because sam does not have its race set assertThat(Optional.of(sam)).get().hasNoNullFields();- Returns:
- a new
AbstractObjectAssertfor assertions chaining on the value of the Optional. - Throws:
AssertionError- if the actualOptionalis null.AssertionError- if the actualOptionalis empty.- Since:
- 3.9.0
-
checkNotNull
private void checkNotNull(Object expectedValue)
-
assertValueIsPresent
private void assertValueIsPresent()
-
-