T - type of the value contained in the Optional.public abstract class AbstractOptionalAssert<S extends AbstractOptionalAssert<S,T>,T> extends AbstractAssert<S,Optional<T>>
Optional.actual, info, myself| Modifier | Constructor and Description |
|---|---|
protected |
AbstractOptionalAssert(Optional<T> actual,
Class<?> selfType) |
| Modifier and Type | Method and Description |
|---|---|
S |
contains(T expectedValue)
Verifies that the actual
Optional contains the given value (alias of hasValue(Object)). |
S |
containsInstanceOf(Class<?> clazz)
Verifies that the actual
Optional contains a value that is an instance of the argument. |
S |
containsSame(T expectedValue)
Verifies that the actual
Optional contains the instance given as an argument (i.e. |
S |
hasValue(T expectedValue)
Verifies that the actual
Optional contains the given value (alias of contains(Object)). |
S |
hasValueSatisfying(Consumer<T> requirement)
|
S |
isEmpty()
Verifies that the actual
Optional is empty. |
S |
isNotEmpty()
Verifies that there is a value present in the actual
Optional, it's an alias of isPresent(). |
S |
isNotPresent()
|
S |
isPresent()
Verifies that there is a value present in the actual
Optional. |
S |
usingDefaultValueComparator()
Revert to standard comparison for incoming assertion
Optional value checks. |
S |
usingFieldByFieldValueComparator()
Use field/property by field/property comparison (including inherited fields/properties) instead of relying on
actual type A
equals method to compare the Optional value's object for incoming assertion
checks. |
S |
usingValueComparator(Comparator<? super T> 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. |
as, as, asList, asString, describedAs, describedAs, descriptionText, doesNotHave, doesNotHaveSameClassAs, equals, failWithMessage, getWritableAssertionInfo, has, hashCode, hasSameClassAs, hasToString, inBinary, inHexadecimal, is, isEqualTo, isExactlyInstanceOf, isIn, isIn, isInstanceOf, isInstanceOfAny, isNot, isNotEqualTo, isNotExactlyInstanceOf, isNotIn, isNotIn, isNotInstanceOf, isNotInstanceOfAny, isNotNull, isNotOfAnyClassIn, isNotSameAs, isNull, isOfAnyClassIn, isSameAs, matches, matches, overridingErrorMessage, satisfies, setCustomRepresentation, throwAssertionError, usingComparator, usingDefaultComparator, withFailMessage, withRepresentation, withThreadDumpOnErrorpublic S isPresent()
Optional.
Assertion will pass :
assertThat(Optional.of("something")).isPresent();
Assertion will fail :
assertThat(Optional.empty()).isPresent();public S isNotEmpty()
Optional, it's an alias of isPresent().
Assertion will pass :
assertThat(Optional.of("something")).isNotEmpty();
Assertion will fail :
assertThat(Optional.empty()).isNotEmpty();public S isEmpty()
Optional is empty.
Assertion will pass :
assertThat(Optional.empty()).isEmpty();
Assertion will fail :
assertThat(Optional.of("something")).isEmpty();public S isNotPresent()
Optional is empty (alias of isEmpty()).
Assertion will pass :
assertThat(Optional.empty()).isNotPresent();
Assertion will fail :
assertThat(Optional.of("something")).isNotPresent();public S contains(T 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 S hasValueSatisfying(Consumer<T> 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 S hasValue(T 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 S 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.public S usingFieldByFieldValueComparator()
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 S usingValueComparator(Comparator<? super T> 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 S usingDefaultValueComparator()
Optional value checks.
This method should be used to disable a custom comparison strategy set by calling
usingValueComparator(Comparator).
this assertion object.public S containsSame(T 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.Copyright © 2014–2016 AssertJ. All rights reserved.