public class BDDAssertions extends Assertions
The difference with the Assertions
class is that entry point methods are named then
instead of
assertThat
.
For example:
@Test
public void bdd_assertions_example() {
//given
List<BasketBallPlayer> bulls = new ArrayList<>();
//when
bulls.add(rose);
bulls.add(noah);
then(bulls).contains(rose, noah).doesNotContain(james);
}
Modifier | Constructor and Description |
---|---|
protected |
BDDAssertions()
Creates a new
. |
Modifier and Type | Method and Description |
---|---|
static <ACTUAL extends Iterable<? extends ELEMENT>,ELEMENT,ELEMENT_ASSERT extends AbstractAssert<ELEMENT_ASSERT,ELEMENT>> |
then(ACTUAL actual,
Class<ELEMENT_ASSERT> assertClass)
Creates a new instance of
allowing to navigate to any Iterable element
in order to perform assertions on it. |
static <T> T |
then(AssertProvider<T> component)
Delegates the creation of the
Assert to the AssertProvider.assertThat() of the given component. |
static AtomicBooleanAssert |
then(AtomicBoolean actual)
Create assertion for
AtomicBoolean . |
static AtomicIntegerAssert |
then(AtomicInteger actual)
Create assertion for
AtomicInteger . |
static AtomicIntegerArrayAssert |
then(AtomicIntegerArray actual)
Create assertion for
AtomicIntegerArray . |
static <OBJECT> AtomicIntegerFieldUpdaterAssert<OBJECT> |
then(AtomicIntegerFieldUpdater<OBJECT> actual)
Create assertion for
AtomicIntegerFieldUpdater . |
static AtomicLongAssert |
then(AtomicLong actual)
Create assertion for
AtomicLong . |
static AtomicLongArrayAssert |
then(AtomicLongArray actual)
Create assertion for
AtomicLongArray . |
static <OBJECT> AtomicLongFieldUpdaterAssert<OBJECT> |
then(AtomicLongFieldUpdater<OBJECT> actual)
Create assertion for
AtomicLongFieldUpdater . |
static <VALUE> AtomicMarkableReferenceAssert<VALUE> |
then(AtomicMarkableReference<VALUE> actual)
Create assertion for
AtomicMarkableReference . |
static <VALUE> AtomicReferenceAssert<VALUE> |
then(AtomicReference<VALUE> actual)
Create assertion for
AtomicReference . |
static <ELEMENT> AtomicReferenceArrayAssert<ELEMENT> |
then(AtomicReferenceArray<ELEMENT> actual)
Create assertion for
AtomicReferenceArray . |
static <FIELD,OBJECT> |
then(AtomicReferenceFieldUpdater<OBJECT,FIELD> actual)
Create assertion for
AtomicReferenceFieldUpdater . |
static <VALUE> AtomicStampedReferenceAssert<VALUE> |
then(AtomicStampedReference<VALUE> actual)
Create assertion for
AtomicStampedReference . |
static AbstractBigDecimalAssert<?> |
then(BigDecimal actual)
Creates a new instance of
. |
static AbstractBigIntegerAssert<?> |
then(BigInteger actual)
Creates a new instance of
. |
static AbstractBooleanAssert<?> |
then(boolean actual)
Creates a new instance of
. |
static AbstractBooleanAssert<?> |
then(Boolean actual)
Creates a new instance of
. |
static AbstractBooleanArrayAssert<?> |
then(boolean[] actual)
Creates a new instance of
. |
static AbstractByteAssert<?> |
then(byte actual)
Creates a new instance of
. |
static AbstractByteAssert<?> |
then(Byte actual)
Creates a new instance of
. |
static AbstractByteArrayAssert<?> |
then(byte[] actual)
Creates a new instance of
. |
static AbstractCharacterAssert<?> |
then(char actual)
Creates a new instance of
. |
static AbstractCharArrayAssert<?> |
then(char[] actual)
Creates a new instance of
. |
static AbstractCharacterAssert<?> |
then(Character actual)
Creates a new instance of
. |
static AbstractCharSequenceAssert<?,? extends CharSequence> |
then(CharSequence actual)
Creates a new instance of
. |
static ClassAssert |
then(Class<?> actual)
Creates a new instance of
|
static <RESULT> CompletableFutureAssert<RESULT> |
then(CompletableFuture<RESULT> future)
Create assertion for
CompletableFuture . |
static <RESULT> CompletableFutureAssert<RESULT> |
then(CompletionStage<RESULT> actual)
Create assertion for
CompletionStage by converting it to a CompletableFuture and returning a CompletableFutureAssert . |
static AbstractDateAssert<?> |
then(Date actual)
Creates a new instance of
. |
static AbstractDoubleAssert<?> |
then(double actual)
Creates a new instance of
. |
static AbstractDoubleAssert<?> |
then(Double actual)
Creates a new instance of
. |
static AbstractDoubleArrayAssert<?> |
then(double[] actual)
Creates a new instance of
. |
static DoublePredicateAssert |
then(DoublePredicate actual)
Create assertion for
DoublePredicate . |
static ListAssert<Double> |
then(DoubleStream actual)
Creates a new instance of
from the given DoubleStream . |
static AbstractFileAssert<?> |
then(File actual)
Creates a new instance of
. |
static AbstractFloatAssert<?> |
then(float actual)
Creates a new instance of
. |
static AbstractFloatAssert<?> |
then(Float actual)
Creates a new instance of
. |
static AbstractFloatArrayAssert<?> |
then(float[] actual)
Creates a new instance of
. |
static <RESULT> FutureAssert<RESULT> |
then(Future<RESULT> actual)
Creates a new instance of
FutureAssert |
static AbstractInputStreamAssert<?,? extends InputStream> |
then(InputStream actual)
Creates a new instance of
. |
static AbstractInstantAssert<?> |
then(Instant actual)
Creates a new instance of
. |
static AbstractIntegerAssert<?> |
then(int actual)
Creates a new instance of
. |
static AbstractIntArrayAssert<?> |
then(int[] actual)
Creates a new instance of
. |
static AbstractIntegerAssert<?> |
then(Integer actual)
Creates a new instance of
. |
static IntPredicateAssert |
then(IntPredicate actual)
Create assertion for
IntPredicate . |
static ListAssert<Integer> |
then(IntStream actual)
Creates a new instance of
from the given IntStream . |
static <ACTUAL extends Iterable<? extends ELEMENT>,ELEMENT,ELEMENT_ASSERT extends AbstractAssert<ELEMENT_ASSERT,ELEMENT>> |
then(Iterable<? extends ELEMENT> actual,
AssertFactory<ELEMENT,ELEMENT_ASSERT> assertFactory)
Creates a new instance of
allowing to navigate to any Iterable element
in order to perform assertions on it. |
static <T> IterableAssert<T> |
then(Iterable<? extends T> actual)
Creates a new instance of
. |
static <T> IteratorAssert<T> |
then(Iterator<? extends T> actual)
Creates a new instance of
. |
static <ACTUAL extends List<? extends ELEMENT>,ELEMENT,ELEMENT_ASSERT extends AbstractAssert<ELEMENT_ASSERT,ELEMENT>> |
then(List<? extends ELEMENT> actual,
AssertFactory<ELEMENT,ELEMENT_ASSERT> assertFactory)
Creates a new instance of
allowing to navigate to any List element
in order to perform assertions on it. |
static <ELEMENT,ACTUAL extends List<? extends ELEMENT>,ELEMENT_ASSERT extends AbstractAssert<ELEMENT_ASSERT,ELEMENT>> |
then(List<? extends ELEMENT> actual,
Class<ELEMENT_ASSERT> assertClass)
Creates a new instance of
tallowing to navigate to any List element
in order to perform assertions on it. |
static <T> ListAssert<T> |
then(List<? extends T> actual)
Creates a new instance of
. |
static AbstractLocalDateAssert<?> |
then(LocalDate actual)
Creates a new instance of
. |
static AbstractLocalDateTimeAssert<?> |
then(LocalDateTime actual)
Creates a new instance of
. |
static AbstractLocalTimeAssert<?> |
then(LocalTime actual)
Creates a new instance of
. |
static AbstractLongAssert<?> |
then(long actual)
Creates a new instance of
. |
static AbstractLongAssert<?> |
then(Long actual)
Creates a new instance of
. |
static AbstractLongArrayAssert<?> |
then(long[] actual)
Creates a new instance of
. |
static LongPredicateAssert |
then(LongPredicate actual)
Create assertion for
LongPredicate . |
static ListAssert<Long> |
then(LongStream actual)
Creates a new instance of
from the given LongStream . |
static <K,V> MapAssert<K,V> |
then(Map<K,V> actual)
Creates a new instance of
. |
static AbstractOffsetDateTimeAssert<?> |
then(OffsetDateTime actual)
Creates a new instance of
. |
static AbstractOffsetTimeAssert<?> |
then(OffsetTime actual)
Creates a new instance of
. |
static <VALUE> OptionalAssert<VALUE> |
then(Optional<VALUE> optional)
Create assertion for
Optional . |
static OptionalDoubleAssert |
then(OptionalDouble optional)
Create assertion for
OptionalDouble . |
static OptionalIntAssert |
then(OptionalInt optional)
Create assertion for
OptionalInt . |
static OptionalLongAssert |
then(OptionalLong optional)
Create assertion for
OptionalLong . |
static AbstractPathAssert<?> |
then(Path actual)
Creates a new instance of
PathAssert |
static <T> PredicateAssert<T> |
then(Predicate<T> actual)
Create assertion for
Predicate . |
static AbstractShortAssert<?> |
then(short actual)
Creates a new instance of
. |
static AbstractShortAssert<?> |
then(Short actual)
Creates a new instance of
. |
static AbstractShortArrayAssert<?> |
then(short[] actual)
Creates a new instance of
. |
static <ELEMENT> ListAssert<ELEMENT> |
then(Stream<? extends ELEMENT> actual)
Creates a new instance of
from the given Stream . |
static AbstractStringAssert<?> |
then(String actual)
Creates a new instance of
. |
static AbstractCharSequenceAssert<?,? extends CharSequence> |
then(StringBuffer actual)
Creates a new instance of
from a StringBuffer . |
static AbstractCharSequenceAssert<?,? extends CharSequence> |
then(StringBuilder actual)
Creates a new instance of
from a StringBuilder . |
static <T> ObjectAssert<T> |
then(T actual)
Creates a new instance of
. |
static <T extends Comparable<? super T>> |
then(T actual)
Creates a new instance of
with
standard comparison semantics. |
static <T extends AssertDelegateTarget> |
then(T assertion)
Returns the given assertion.
|
static <T> ObjectArrayAssert<T> |
then(T[] actual)
Creates a new instance of
. |
static AbstractThrowableAssert<?,? extends Throwable> |
then(Throwable actual)
Creates a new instance of
. |
static AbstractUriAssert<?> |
then(URI actual)
Creates a new instance of
. |
static AbstractUrlAssert<?> |
then(URL actual)
Creates a new instance of
. |
static AbstractZonedDateTimeAssert<?> |
then(ZonedDateTime actual)
Creates a new instance of
. |
static AbstractThrowableAssert<?,? extends Throwable> |
thenCode(ThrowableAssert.ThrowingCallable shouldRaiseOrNotThrowable)
Allows to capture and then assert on a
Throwable more easily when used with Java 8 lambdas. |
static <T> ObjectAssert<T> |
thenObject(T actual)
Creates a new instance of
for any object. |
static AbstractThrowableAssert<?,? extends Throwable> |
thenThrownBy(ThrowableAssert.ThrowingCallable shouldRaiseThrowable)
Allows to capture and then assert on a
Throwable (easier done with lambdas). |
static AbstractThrowableAssert<?,? extends Throwable> |
thenThrownBy(ThrowableAssert.ThrowingCallable shouldRaiseThrowable,
String description,
Object... args)
Allows to capture and then assert on a
Throwable like thenThrownBy(ThrowingCallable) but this method
let you set the assertion description the same way you do with as(String, Object...) . |
allOf, allOf, anyOf, anyOf, assertThat, assertThat, assertThat, assertThat, assertThat, assertThat, assertThat, assertThat, assertThat, assertThat, assertThat, assertThat, assertThat, assertThat, assertThat, assertThat, assertThat, assertThat, assertThat, assertThat, assertThat, assertThat, assertThat, assertThat, assertThat, assertThat, assertThat, assertThat, assertThat, assertThat, assertThat, assertThat, assertThat, assertThat, assertThat, assertThat, assertThat, assertThat, assertThat, assertThat, assertThat, assertThat, assertThat, assertThat, assertThat, assertThat, assertThat, assertThat, assertThat, assertThat, assertThat, assertThat, assertThat, assertThat, assertThat, assertThat, assertThat, assertThat, assertThat, assertThat, assertThat, assertThat, assertThat, assertThat, assertThat, assertThat, assertThat, assertThat, assertThat, assertThat, assertThat, assertThat, assertThat, assertThat, assertThat, assertThat, assertThat, assertThat, assertThat, assertThat, assertThat, assertThat, assertThat, assertThat, assertThat, assertThatCode, assertThatExceptionOfType, assertThatIllegalArgumentException, assertThatIllegalStateException, assertThatIOException, assertThatNullPointerException, assertThatObject, assertThatThrownBy, assertThatThrownBy, atIndex, byLessThan, byLessThan, byLessThan, byLessThan, byLessThan, byLessThan, byLessThan, byLessThan, byLessThan, catchThrowable, catchThrowableOfType, contentOf, contentOf, contentOf, contentOf, contentOf, contentOf, doesNotHave, entry, extractProperty, extractProperty, fail, fail, fail, failBecauseExceptionWasNotThrown, filter, filter, from, in, linesOf, linesOf, linesOf, linesOf, linesOf, linesOf, not, not, notIn, offset, offset, registerCustomDateFormat, registerCustomDateFormat, registerFormatterForType, setAllowComparingPrivateFields, setAllowExtractingPrivateFields, setExtractBareNamePropertyMethods, setLenientDateParsing, setMaxElementsForPrinting, setMaxLengthForSingleLineDescription, setRemoveAssertJRelatedElementsFromStackTrace, shouldHaveThrown, tuple, useDefaultDateFormatsOnly, useDefaultRepresentation, useRepresentation, within, within, within, within, within, within, within, within, within, withinPercentage, withinPercentage, withinPercentage, withPrecision, withPrecision
protected BDDAssertions()
BDDAssertions
.public static <T> PredicateAssert<T> then(Predicate<T> actual)
Predicate
.T
- the type of the value contained in the Predicate
.actual
- the actual value.public static IntPredicateAssert then(IntPredicate actual)
IntPredicate
.actual
- the actual value.public static LongPredicateAssert then(LongPredicate actual)
LongPredicate
.actual
- the actual value.public static DoublePredicateAssert then(DoublePredicate actual)
DoublePredicate
.actual
- the actual value.public static <VALUE> OptionalAssert<VALUE> then(Optional<VALUE> optional)
Optional
.VALUE
- the type of the value contained in the Optional
.optional
- the actual value.public static OptionalIntAssert then(OptionalInt optional)
OptionalInt
.optional
- the actual value.public static OptionalLongAssert then(OptionalLong optional)
OptionalLong
.optional
- the actual value.public static OptionalDoubleAssert then(OptionalDouble optional)
OptionalDouble
.optional
- the actual value.public static AbstractBigDecimalAssert<?> then(BigDecimal actual)
BigDecimalAssert
.actual
- the actual value.public static AbstractBigIntegerAssert<?> then(BigInteger actual)
BigIntegerAssert
.actual
- the actual value.public static AbstractBooleanAssert<?> then(boolean actual)
BooleanAssert
.actual
- the actual value.public static AbstractBooleanAssert<?> then(Boolean actual)
BooleanAssert
.actual
- the actual value.public static AbstractBooleanArrayAssert<?> then(boolean[] actual)
BooleanArrayAssert
.actual
- the actual value.public static AbstractByteAssert<?> then(byte actual)
ByteAssert
.actual
- the actual value.public static AbstractByteAssert<?> then(Byte actual)
ByteAssert
.actual
- the actual value.public static AbstractByteArrayAssert<?> then(byte[] actual)
ByteArrayAssert
.actual
- the actual value.public static AbstractCharacterAssert<?> then(char actual)
CharacterAssert
.actual
- the actual value.public static AbstractCharArrayAssert<?> then(char[] actual)
CharArrayAssert
.actual
- the actual value.public static AbstractCharacterAssert<?> then(Character actual)
CharacterAssert
.actual
- the actual value.public static ClassAssert then(Class<?> actual)
ClassAssert
actual
- the actual value.public static <T extends Comparable<? super T>> AbstractComparableAssert<?,T> then(T actual)
GenericComparableAssert
with
standard comparison semantics.T
- the actual typeactual
- the actual value.public static <T> IterableAssert<T> then(Iterable<? extends T> actual)
IterableAssert
.T
- the actual elements typeactual
- the actual value.public static <T> IteratorAssert<T> then(Iterator<? extends T> actual)
IteratorAssert
.
Breaking change in version 3.12.0: this method does not return anymore an IterableAssert
but an IteratorAssert
.
In order to access assertions from IterableAssert
, use AbstractIteratorAssert.toIterable()
.
IteratorAssert
instances have limited assertions because it does not consume iterator's elements.
Examples:
Iterator<String> bestBasketBallPlayers = getBestBasketBallPlayers();
then(bestBasketBallPlayers).hasNext() // Iterator assertion
.toIterable() // switch to Iterable assertions
.contains("Jordan", "Magic", "Lebron"); // Iterable assertion
T
- the actual elements typeactual
- the actual value.public static <ACTUAL extends Iterable<? extends ELEMENT>,ELEMENT,ELEMENT_ASSERT extends AbstractAssert<ELEMENT_ASSERT,ELEMENT>> FactoryBasedNavigableIterableAssert<?,ACTUAL,ELEMENT,ELEMENT_ASSERT> then(Iterable<? extends ELEMENT> actual, AssertFactory<ELEMENT,ELEMENT_ASSERT> assertFactory)
FactoryBasedNavigableIterableAssert
allowing to navigate to any Iterable
element
in order to perform assertions on it.
Navigational methods provided:
The available assertions after navigating to an element depend on the ELEMENT_ASSERT
parameter of the given
AssertFactory<ELEMENT, ELEMENT_ASSERT>
(AssertJ can't figure it out because of Java type erasure).
Example with String
element assertions:
Iterable<String> hobbits = newHashSet("frodo", "sam", "pippin");
// build an AssertFactory for StringAssert (much nicer with Java 8 lambdas)
AssertFactory<String, StringAssert> stringAssertFactory = new AssertFactory<String, StringAssert>() {
@Override
public StringAssert createAssert(String string) {
return new StringAssert(string);
}
};
// assertion succeeds with String assertions chained after first()
then(hobbits, stringAssertFactory).first()
.startsWith("fro")
.endsWith("do");
ACTUAL
- The actual typeELEMENT
- The actual elements typeELEMENT_ASSERT
- The actual elements AbstractAssert typeactual
- the actual value.assertFactory
- the factory used to create the elements assert instance.public static <ACTUAL extends Iterable<? extends ELEMENT>,ELEMENT,ELEMENT_ASSERT extends AbstractAssert<ELEMENT_ASSERT,ELEMENT>> ClassBasedNavigableIterableAssert<?,ACTUAL,ELEMENT,ELEMENT_ASSERT> then(ACTUAL actual, Class<ELEMENT_ASSERT> assertClass)
ClassBasedNavigableIterableAssert
allowing to navigate to any Iterable
element
in order to perform assertions on it.
Navigational methods provided:
The available assertions after navigating to an element depend on the given assertClass
(AssertJ can't find the element assert type by itself because of Java type erasure).
Example with String
element assertions:
Iterable<String> hobbits = newHashSet("frodo", "sam", "pippin");
// assertion succeeds with String assertions chained after first()
then(hobbits, StringAssert.class).first()
.startsWith("fro")
.endsWith("do");
ACTUAL
- The actual typeELEMENT
- The actual elements typeELEMENT_ASSERT
- The actual elements AbstractAssert typeactual
- the actual value.assertClass
- the class used to create the elements assert instance.public static <ACTUAL extends List<? extends ELEMENT>,ELEMENT,ELEMENT_ASSERT extends AbstractAssert<ELEMENT_ASSERT,ELEMENT>> FactoryBasedNavigableListAssert<?,ACTUAL,ELEMENT,ELEMENT_ASSERT> then(List<? extends ELEMENT> actual, AssertFactory<ELEMENT,ELEMENT_ASSERT> assertFactory)
FactoryBasedNavigableListAssert
allowing to navigate to any List
element
in order to perform assertions on it.
Navigational methods provided:
The available assertions after navigating to an element depend on the ELEMENT_ASSERT
parameter of the given
AssertFactory<ELEMENT, ELEMENT_ASSERT>
(AssertJ can't figure it out because of Java type erasure).
Example with String
element assertions:
List<String> hobbits = newArrayList("frodo", "sam", "pippin");
// build an AssertFactory for StringAssert (much nicer with Java 8 lambdas)
AssertFactory<String, StringAssert> stringAssertFactory = new AssertFactory<String, StringAssert>() {
@Override
public StringAssert createAssert(String string) {
return new StringAssert(string);
}
};
// assertion succeeds with String assertions chained after first()
then(hobbits, stringAssertFactory).first()
.startsWith("fro")
.endsWith("do");
ACTUAL
- The actual typeELEMENT
- The actual elements typeELEMENT_ASSERT
- The actual elements AbstractAssert typeactual
- the actual value.assertFactory
- the factory used to create the elements assert instance.public static <ELEMENT,ACTUAL extends List<? extends ELEMENT>,ELEMENT_ASSERT extends AbstractAssert<ELEMENT_ASSERT,ELEMENT>> ClassBasedNavigableListAssert<?,ACTUAL,ELEMENT,ELEMENT_ASSERT> then(List<? extends ELEMENT> actual, Class<ELEMENT_ASSERT> assertClass)
ClassBasedNavigableListAssert
tallowing to navigate to any List
element
in order to perform assertions on it.
Navigational methods provided:
The available assertions after navigating to an element depend on the given assertClass
(AssertJ can't find the element assert type by itself because of Java type erasure).
Example with String
element assertions:
List<String> hobbits = newArrayList("frodo", "sam", "pippin");
// assertion succeeds with String assertions chained after first()
then(hobbits, StringAssert.class).first()
.startsWith("fro")
.endsWith("do");
ACTUAL
- The actual typeELEMENT
- The actual elements typeELEMENT_ASSERT
- The actual elements AbstractAssert typeactual
- the actual value.assertClass
- the class used to create the elements assert instance.public static AbstractDoubleAssert<?> then(double actual)
DoubleAssert
.actual
- the actual value.public static AbstractDoubleAssert<?> then(Double actual)
DoubleAssert
.actual
- the actual value.public static AbstractDoubleArrayAssert<?> then(double[] actual)
DoubleArrayAssert
.actual
- the actual value.public static AbstractFileAssert<?> then(File actual)
FileAssert
.actual
- the actual value.public static AbstractPathAssert<?> then(Path actual)
PathAssert
actual
- the path to testpublic static <RESULT> FutureAssert<RESULT> then(Future<RESULT> actual)
FutureAssert
RESULT
- the type of the value contained in the Future
.actual
- the future to testpublic static AbstractInputStreamAssert<?,? extends InputStream> then(InputStream actual)
InputStreamAssert
.actual
- the actual value.public static AbstractFloatAssert<?> then(float actual)
FloatAssert
.actual
- the actual value.public static AbstractFloatAssert<?> then(Float actual)
FloatAssert
.actual
- the actual value.public static AbstractFloatArrayAssert<?> then(float[] actual)
FloatArrayAssert
.actual
- the actual value.public static AbstractIntegerAssert<?> then(int actual)
IntegerAssert
.actual
- the actual value.public static AbstractIntArrayAssert<?> then(int[] actual)
IntArrayAssert
.actual
- the actual value.public static AbstractIntegerAssert<?> then(Integer actual)
IntegerAssert
.actual
- the actual value.public static <T> ListAssert<T> then(List<? extends T> actual)
ListAssert
.T
- the type of elements.actual
- the actual value.public static AbstractLongAssert<?> then(long actual)
LongAssert
.actual
- the actual value.public static AbstractLongAssert<?> then(Long actual)
LongAssert
.actual
- the actual value.public static AbstractLongArrayAssert<?> then(long[] actual)
LongArrayAssert
.actual
- the actual value.public static <T> ObjectAssert<T> then(T actual)
ObjectAssert
.T
- the type of the actual value.actual
- the actual value.public static <T> ObjectArrayAssert<T> then(T[] actual)
ObjectArrayAssert
.T
- the actual's elements type.actual
- the actual value.public static <K,V> MapAssert<K,V> then(Map<K,V> actual)
MapAssert
.K
- the type of keys in the map.V
- the type of values in the map.actual
- the actual value.public static AbstractShortAssert<?> then(short actual)
ShortAssert
.actual
- the actual value.public static AbstractShortAssert<?> then(Short actual)
ShortAssert
.actual
- the actual value.public static AbstractShortArrayAssert<?> then(short[] actual)
ShortArrayAssert
.actual
- the actual value.public static AbstractCharSequenceAssert<?,? extends CharSequence> then(CharSequence actual)
CharSequenceAssert
.actual
- the actual value.public static AbstractCharSequenceAssert<?,? extends CharSequence> then(StringBuilder actual)
CharSequenceAssert
from a StringBuilder
.actual
- the actual value.public static AbstractCharSequenceAssert<?,? extends CharSequence> then(StringBuffer actual)
CharSequenceAssert
from a StringBuffer
.actual
- the actual value.public static AbstractStringAssert<?> then(String actual)
StringAssert
.actual
- the actual value.public static AbstractDateAssert<?> then(Date actual)
DateAssert
.actual
- the actual value.public static AtomicBooleanAssert then(AtomicBoolean actual)
AtomicBoolean
.actual
- the actual value.public static AtomicIntegerAssert then(AtomicInteger actual)
AtomicInteger
.actual
- the actual value.public static AtomicIntegerArrayAssert then(AtomicIntegerArray actual)
AtomicIntegerArray
.actual
- the actual value.public static <OBJECT> AtomicIntegerFieldUpdaterAssert<OBJECT> then(AtomicIntegerFieldUpdater<OBJECT> actual)
AtomicIntegerFieldUpdater
.OBJECT
- the type of the object holding the updatable field.actual
- the actual value.public static AtomicLongAssert then(AtomicLong actual)
AtomicLong
.actual
- the actual value.public static AtomicLongArrayAssert then(AtomicLongArray actual)
AtomicLongArray
.actual
- the actual value.public static <OBJECT> AtomicLongFieldUpdaterAssert<OBJECT> then(AtomicLongFieldUpdater<OBJECT> actual)
AtomicLongFieldUpdater
.OBJECT
- the type of the object holding the updatable field.actual
- the actual value.public static <VALUE> AtomicReferenceAssert<VALUE> then(AtomicReference<VALUE> actual)
AtomicReference
.VALUE
- the type of the value contained in the AtomicReference
.actual
- the actual value.public static <ELEMENT> AtomicReferenceArrayAssert<ELEMENT> then(AtomicReferenceArray<ELEMENT> actual)
AtomicReferenceArray
.ELEMENT
- the type of the value contained in the AtomicReferenceArray
.actual
- the actual value.public static <FIELD,OBJECT> AtomicReferenceFieldUpdaterAssert<FIELD,OBJECT> then(AtomicReferenceFieldUpdater<OBJECT,FIELD> actual)
AtomicReferenceFieldUpdater
.FIELD
- the type of the field which gets updated by the AtomicReferenceFieldUpdater
.OBJECT
- the type of the object holding the updatable field.actual
- the actual value.public static <VALUE> AtomicMarkableReferenceAssert<VALUE> then(AtomicMarkableReference<VALUE> actual)
AtomicMarkableReference
.VALUE
- the type of the value contained in the AtomicMarkableReference
.actual
- the actual value.public static <VALUE> AtomicStampedReferenceAssert<VALUE> then(AtomicStampedReference<VALUE> actual)
AtomicStampedReference
.VALUE
- the type of the value contained in the AtomicStampedReference
.actual
- the actual value.public static AbstractThrowableAssert<?,? extends Throwable> then(Throwable actual)
ThrowableAssert
.actual
- the actual value.public static AbstractThrowableAssert<?,? extends Throwable> thenThrownBy(ThrowableAssert.ThrowingCallable shouldRaiseThrowable)
Throwable
(easier done with lambdas).
Example :
@Test
public void testException() {
thenThrownBy(() -> { throw new Exception("boom!") }).isInstanceOf(Exception.class)
.hasMessageContaining("boom");
}
If the provided ThrowableAssert.ThrowingCallable
does not raise an exception, an error is immediately thrown,
in that case the test description provided with as(String, Object...)
is not honored.Assertions.catchThrowable(ThrowableAssert.ThrowingCallable)
as shown below:
// assertion will fail but "display me" won't appear in the error
thenThrownBy(() -> {}).as("display me")
.isInstanceOf(Exception.class);
// assertion will fail AND "display me" will appear in the error
Throwable thrown = catchThrowable(() -> {});
assertThat(thrown).as("display me")
.isInstanceOf(Exception.class);
Alternatively you can also use assertThatCode(ThrowingCallable)
for the test description provided
with as(String, Object...)
to always be honored.shouldRaiseThrowable
- The ThrowableAssert.ThrowingCallable
or lambda with the code that should raise the throwable.ThrowableAssert
.public static AbstractThrowableAssert<?,? extends Throwable> thenThrownBy(ThrowableAssert.ThrowingCallable shouldRaiseThrowable, String description, Object... args)
Throwable
like thenThrownBy(ThrowingCallable)
but this method
let you set the assertion description the same way you do with as(String, Object...)
.
Example:
@Test
public void testException() {
// if this assertion failed (but it doesn't), the error message would start with [Test explosive code]
thenThrownBy(() -> { throw new IOException("boom!") }, "Test explosive code")
.isInstanceOf(IOException.class)
.hasMessageContaining("boom");
}
If the provided ThrowingCallable
does not raise an exception, an error is immediately thrown.
The test description provided is honored but not the one with as(String, Object...)
, example:
// assertion will fail but "display me" won't appear in the error message
thenThrownBy(() -> {}).as("display me")
.isInstanceOf(Exception.class);
// assertion will fail AND "display me" will appear in the error message
thenThrownBy(() -> {}, "display me").isInstanceOf(Exception.class);
shouldRaiseThrowable
- The ThrowableAssert.ThrowingCallable
or lambda with the code that should raise the throwable.description
- the new description to set.args
- optional parameter if description is a format String.ThrowableAssert
.public static AbstractThrowableAssert<?,? extends Throwable> thenCode(ThrowableAssert.ThrowingCallable shouldRaiseOrNotThrowable)
Throwable
more easily when used with Java 8 lambdas.
Example :
ThrowingCallable callable = () -> {
throw new Exception("boom!");
};
// assertion succeeds
thenCode(callable).isInstanceOf(Exception.class)
.hasMessageContaining("boom");
// assertion fails
thenCode(callable).doesNotThrowAnyException();
If the provided ThrowableAssert.ThrowingCallable
does not validate against next assertions, an error is immediately raised,
in that case the test description provided with as(String, Object...)
is not honored.Assertions.catchThrowable(ThrowableAssert.ThrowingCallable)
as shown below.
ThrowingCallable doNothing = () -> {
// do nothing
};
// assertion fails and "display me" appears in the assertion error
thenCode(doNothing).as("display me")
.isInstanceOf(Exception.class);
// assertion will fail AND "display me" will appear in the error
Throwable thrown = catchThrowable(doNothing);
thenCode(thrown).as("display me")
.isInstanceOf(Exception.class);
This method was not named then
because the java compiler reported it ambiguous when used directly with a lambda :(
shouldRaiseOrNotThrowable
- The ThrowableAssert.ThrowingCallable
or lambda with the code that should raise the throwable.null
if none was raised by the callable.public static <T> ObjectAssert<T> thenObject(T actual)
ObjectAssert
for any object.
This overload is useful, when an overloaded method of then(...) takes precedence over the generic then(Object)
.
Example:
Cast necessary because then(List)
"forgets" actual type:
then(new LinkedList<>(asList("abc"))).matches(list -> ((Deque<String>) list).getFirst().equals("abc"));
No cast needed, but also no additional list assertions:
thenObject(new LinkedList<>(asList("abc"))).matches(list -> list.getFirst().equals("abc"));
T
- the type of the actual value.actual
- the actual value.public static AbstractLocalDateAssert<?> then(LocalDate actual)
LocalDateAssert
.actual
- the actual value.public static AbstractLocalDateTimeAssert<?> then(LocalDateTime actual)
LocalDateTimeAssert
.actual
- the actual value.public static AbstractZonedDateTimeAssert<?> then(ZonedDateTime actual)
ZonedDateTimeAssert
.actual
- the actual value.public static AbstractLocalTimeAssert<?> then(LocalTime actual)
LocalTimeAssert
.actual
- the actual value.public static AbstractOffsetTimeAssert<?> then(OffsetTime actual)
OffsetTimeAssert
.actual
- the actual value.public static AbstractInstantAssert<?> then(Instant actual)
InstantAssert
.actual
- the actual value.public static AbstractUriAssert<?> then(URI actual)
UriAssert
.actual
- the actual value.public static AbstractUrlAssert<?> then(URL actual)
UrlAssert
.actual
- the actual value.public static AbstractOffsetDateTimeAssert<?> then(OffsetDateTime actual)
OffsetTimeAssert
.actual
- the actual value.public static <RESULT> CompletableFutureAssert<RESULT> then(CompletableFuture<RESULT> future)
CompletableFuture
.RESULT
- the type of the value contained in the CompletableFuture
.future
- the actual value.public static <RESULT> CompletableFutureAssert<RESULT> then(CompletionStage<RESULT> actual)
CompletionStage
by converting it to a CompletableFuture
and returning a CompletableFutureAssert
.
If the given CompletionStage
is null, the CompletableFuture
in the returned CompletableFutureAssert
will also be null.
RESULT
- the type of the value contained in the CompletionStage
.actual
- the actual value.public static <T extends AssertDelegateTarget> T then(T assertion)
then
.
Consider for example the following MyButton and MyButtonAssert classes:
public class MyButton extends JButton {
private boolean blinking;
public boolean isBlinking() { return this.blinking; }
public void setBlinking(boolean blink) { this.blinking = blink; }
}
private static class MyButtonAssert implements AssertDelegateTarget {
private MyButton button;
MyButtonAssert(MyButton button) { this.button = button; }
void isBlinking() {
// standard assertion from core Assertions.then
then(button.isBlinking()).isTrue();
}
void isNotBlinking() {
// standard assertion from core Assertions.then
then(button.isBlinking()).isFalse();
}
}
As MyButtonAssert implements AssertDelegateTarget, you can use then(buttonAssert).isBlinking();
instead of buttonAssert.isBlinking();
to have easier to read assertions:
@Test
public void AssertDelegateTarget_example() {
MyButton button = new MyButton();
MyButtonAssert buttonAssert = new MyButtonAssert(button);
// you can encapsulate MyButtonAssert assertions methods within then
then(buttonAssert).isNotBlinking(); // same as : buttonAssert.isNotBlinking();
button.setBlinking(true);
then(buttonAssert).isBlinking(); // same as : buttonAssert.isBlinking();
}
T
- the generic type of the user-defined assert.assertion
- the assertion to return.public static <T> T then(AssertProvider<T> component)
Assert
to the AssertProvider.assertThat()
of the given component.
Read the comments on AssertProvider
for an example of its usage.
T
- the AssertProvider wrapped type.component
- the component that creates its own assertAssert
of the given componentpublic static <ELEMENT> ListAssert<ELEMENT> then(Stream<? extends ELEMENT> actual)
ListAssert
from the given Stream
.
Be aware that the Stream
under test will be converted to a List
when an assertions require to inspect its content.
Once this is done the Stream
can't reused as it would have been consumed.
Calling multiple methods on the returned ListAssert
is safe as it only interacts with the List
built from the Stream
.
Examples:
// you can chain multiple assertions on the Stream as it is converted to a List
then(Stream.of(1, 2, 3)).contains(1)
.doesNotContain(42);
The following assertion fails as the Stream under test is converted to a List before being compared to the expected Stream:
// FAIL: the Stream under test is converted to a List and compared to a Stream but a List is not a Stream.
then(Stream.of(1, 2, 3)).isEqualTo(Stream.of(1, 2, 3));
These assertions succeed as isEqualTo
and isSameAs
checks references which does not require to convert the Stream to a List.
// The following assertions succeed as it only performs reference checking which does not require to convert the Stream to a List
Stream<Integer> stream = Stream.of(1, 2, 3);
then(stream).isEqualTo(stream)
.isSameAs(stream);
ELEMENT
- the type of elements.actual
- the actual Stream
value.public static ListAssert<Double> then(DoubleStream actual)
ListAssert
from the given DoubleStream
.
Be aware that the DoubleStream
under test will be converted to a List
when an assertions require to inspect its content.
Once this is done the DoubleStream
can't reused as it would have been consumed.
Calling multiple methods on the returned ListAssert
is safe as it only interacts with the List
built from the DoubleStream
.
Examples:
// you can chain multiple assertions on the DoubleStream as it is converted to a List
then(DoubleStream.of(1.0, 2.0, 3.0)).contains(1.0)
.doesNotContain(42.0);
The following assertion fails as the DoubleStream under test is converted to a List before being compared to the expected DoubleStream:
// FAIL: the DoubleStream under test is converted to a List and compared to a DoubleStream but a List is not a DoubleStream.
then(DoubleStream.of(1.0, 2.0, 3.0)).isEqualTo(DoubleStream.of(1.0, 2.0, 3.0));
These assertions succeed as isEqualTo
and isSameAs
checks references which does not require to convert the DoubleStream to a List.
// The following assertions succeed as it only performs reference checking which does not require to convert the DoubleStream to a List
DoubleStream stream = DoubleStream.of(1.0, 2.0, 3.0);
then(stream).isEqualTo(stream)
.isSameAs(stream);
actual
- the actual DoubleStream
value.public static ListAssert<Long> then(LongStream actual)
ListAssert
from the given LongStream
.
Be aware that the LongStream
under test will be converted to a List
when an assertions require to inspect its content.
Once this is done the LongStream
can't reused as it would have been consumed.
Calling multiple methods on the returned ListAssert
is safe as it only interacts with the List
built from the LongStream
.
Examples:
// you can chain multiple assertions on the LongStream as it is converted to a List
then(LongStream.of(1, 2, 3)).contains(1)
.doesNotContain(42);
The following assertion fails as the LongStream under test is converted to a List before being compared to the expected LongStream:
// FAIL: the LongStream under test is converted to a List and compared to a LongStream but a List is not a LongStream.
then(LongStream.of(1, 2, 3)).isEqualTo(LongStream.of(1, 2, 3));
These assertions succeed as isEqualTo
and isSameAs
checks references which does not require to convert the LongStream to a List.
// The following assertions succeed as it only performs reference checking which does not require to convert the LongStream to a List
LongStream stream = LongStream.of(1, 2, 3);
then(stream).isEqualTo(stream)
.isSameAs(stream);
actual
- the actual LongStream
value.public static ListAssert<Integer> then(IntStream actual)
ListAssert
from the given IntStream
.
Be aware that the IntStream
under test will be converted to a List
when an assertions require to inspect its content.
Once this is done the IntStream
can't reused as it would have been consumed.
Calling multiple methods on the returned ListAssert
is safe as it only interacts with the List
built from the IntStream
.
Examples:
// you can chain multiple assertions on the IntStream as it is converted to a List
then(IntStream.of(1, 2, 3)).contains(1)
.doesNotContain(42);
The following assertion fails as the IntStream under test is converted to a List before being compared to the expected IntStream:
// FAIL: the IntStream under test is converted to a List and compared to a IntStream but a List is not a IntStream.
then(IntStream.of(1, 2, 3)).isEqualTo(IntStream.of(1, 2, 3));
These assertions succeed as isEqualTo
and isSameAs
checks references which does not require to convert the IntStream to a List.
// The following assertions succeed as it only performs reference checking which does not require to convert the IntStream to a List
IntStream stream = IntStream.of(1, 2, 3);
then(stream).isEqualTo(stream)
.isSameAs(stream);
actual
- the actual IntStream
value.Copyright © 2014–2019 AssertJ. All rights reserved.