Class BDDAssertions
- java.lang.Object
-
- org.assertj.core.api.Assertions
-
- org.assertj.core.api.BDDAssertions
-
- All Implemented Interfaces:
InstanceOfAssertFactories
public class BDDAssertions extends Assertions
Behavior-driven development style entry point for assertion methods for different types. Each method in this class is a static factory for a type-specific assertion object.The difference with the
Assertions
class is that entry point methods are namedthen
instead ofassertThat
.For example:
Use@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); }
to avoid clash with other libraries (like BDDMockito) exposing 'and
then(object)
'. You might have to ignore a warning like: The static method BDDAssertions.then() should be accessed in a static way.- Author:
- Gonzalo Müller, Alex Ruiz, Yvonne Wang, David DIDIER, Ted Young, Joel Costigliola, Matthieu Baechler, Mikhail Mazursky, Nicolas François, Julien Meddah, William Delanoue, Mariusz Smykula
-
-
Field Summary
Fields Modifier and Type Field Description static BDDAssertions
and
ABDDAssertions
which allows to blend assertions with other libraries when the name 'then
' cause clash.-
Fields inherited from interface org.assertj.core.api.InstanceOfAssertFactories
ARRAY, ARRAY_2D, ATOMIC_BOOLEAN, ATOMIC_INTEGER, ATOMIC_INTEGER_ARRAY, ATOMIC_INTEGER_FIELD_UPDATER, ATOMIC_LONG, ATOMIC_LONG_ARRAY, ATOMIC_LONG_FIELD_UPDATER, ATOMIC_MARKABLE_REFERENCE, ATOMIC_REFERENCE, ATOMIC_REFERENCE_ARRAY, ATOMIC_REFERENCE_FIELD_UPDATER, ATOMIC_STAMPED_REFERENCE, BIG_DECIMAL, BIG_INTEGER, BOOLEAN, BOOLEAN_2D_ARRAY, BOOLEAN_ARRAY, BYTE, BYTE_2D_ARRAY, BYTE_ARRAY, CHAR_2D_ARRAY, CHAR_ARRAY, CHAR_SEQUENCE, CHARACTER, CLASS, COMPLETABLE_FUTURE, COMPLETION_STAGE, DATE, DOUBLE, DOUBLE_2D_ARRAY, DOUBLE_ARRAY, DOUBLE_PREDICATE, DOUBLE_STREAM, DURATION, FILE, FLOAT, FLOAT_2D_ARRAY, FLOAT_ARRAY, FUTURE, INPUT_STREAM, INSTANT, INT_2D_ARRAY, INT_ARRAY, INT_PREDICATE, INT_STREAM, INTEGER, ITERABLE, ITERATOR, LIST, LOCAL_DATE, LOCAL_DATE_TIME, LOCAL_TIME, LONG, LONG_2D_ARRAY, LONG_ADDER, LONG_ARRAY, LONG_PREDICATE, LONG_STREAM, MAP, OFFSET_DATE_TIME, OFFSET_TIME, OPTIONAL, OPTIONAL_DOUBLE, OPTIONAL_INT, OPTIONAL_LONG, PATH, PERIOD, PREDICATE, SHORT, SHORT_2D_ARRAY, SHORT_ARRAY, SPLITERATOR, STREAM, STRING, STRING_BUFFER, STRING_BUILDER, THROWABLE, URI_TYPE, URL_TYPE, ZONED_DATE_TIME
-
-
Constructor Summary
Constructors Modifier Constructor Description protected
BDDAssertions()
Creates a new
.BDDAssertions
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static AbstractBooleanAssert<?>
then(boolean actual)
Creates a new instance of
.BooleanAssert
static AbstractBooleanArrayAssert<?>
then(boolean[] actual)
Creates a new instance of
.BooleanArrayAssert
static Boolean2DArrayAssert
then(boolean[][] actual)
Creates a new instance of
.Boolean2DArrayAssert
static AbstractByteAssert<?>
then(byte actual)
Creates a new instance of
.ByteAssert
static AbstractByteArrayAssert<?>
then(byte[] actual)
Creates a new instance of
.ByteArrayAssert
static Byte2DArrayAssert
then(byte[][] actual)
Creates a new instance of
.Byte2DArrayAssert
static AbstractCharacterAssert<?>
then(char actual)
Creates a new instance of
.CharacterAssert
static AbstractCharArrayAssert<?>
then(char[] actual)
Creates a new instance of
.CharArrayAssert
static Char2DArrayAssert
then(char[][] actual)
Creates a new instance of
.Char2DArrayAssert
static AbstractDoubleAssert<?>
then(double actual)
Creates a new instance of
.DoubleAssert
static AbstractDoubleArrayAssert<?>
then(double[] actual)
Creates a new instance of
.DoubleArrayAssert
static Double2DArrayAssert
then(double[][] actual)
Creates a new instance of
.Double2DArrayAssert
static AbstractFloatAssert<?>
then(float actual)
Creates a new instance of
.FloatAssert
static AbstractFloatArrayAssert<?>
then(float[] actual)
Creates a new instance of
.FloatArrayAssert
static Float2DArrayAssert
then(float[][] actual)
Creates a new instance of
.Float2DArrayAssert
static AbstractIntegerAssert<?>
then(int actual)
Creates a new instance of
.IntegerAssert
static AbstractIntArrayAssert<?>
then(int[] actual)
Creates a new instance of
.IntArrayAssert
static Int2DArrayAssert
then(int[][] actual)
Creates a new instance of
.Int2DArrayAssert
static AbstractLongAssert<?>
then(long actual)
Creates a new instance of
.LongAssert
static AbstractLongArrayAssert<?>
then(long[] actual)
Creates a new instance of
.LongArrayAssert
static Long2DArrayAssert
then(long[][] actual)
Creates a new instance of
.Long2DArrayAssert
static AbstractShortAssert<?>
then(short actual)
Creates a new instance of
.ShortAssert
static AbstractShortArrayAssert<?>
then(short[] actual)
Creates a new instance of
.ShortArrayAssert
static Short2DArrayAssert
then(short[][] actual)
Creates a new instance of
.Short2DArrayAssert
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)
Creates a new instance of
allowing to navigate to anyClassBasedNavigableIterableAssert
Iterable
element in order to perform assertions on it.static AbstractFileAssert<?>
then(File actual)
Creates a new instance of
.FileAssert
static AbstractInputStreamAssert<?,? extends InputStream>
then(InputStream actual)
Creates a new instance of
.InputStreamAssert
static AbstractBooleanAssert<?>
then(Boolean actual)
Creates a new instance of
.BooleanAssert
static AbstractByteAssert<?>
then(Byte actual)
Creates a new instance of
.ByteAssert
static AbstractCharacterAssert<?>
then(Character actual)
Creates a new instance of
.CharacterAssert
static AbstractCharSequenceAssert<?,? extends CharSequence>
then(CharSequence actual)
Creates a new instance of
.CharSequenceAssert
static ClassAssert
then(Class<?> actual)
Creates a new instance ofClassAssert
static AbstractDoubleAssert<?>
then(Double actual)
Creates a new instance of
.DoubleAssert
static AbstractFloatAssert<?>
then(Float actual)
Creates a new instance of
.FloatAssert
static AbstractIntegerAssert<?>
then(Integer actual)
Creates a new instance of
.IntegerAssert
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)
Creates a new instance of
allowing to navigate to anyFactoryBasedNavigableIterableAssert
Iterable
element in order to perform assertions on it.static <T> IterableAssert<T>
then(Iterable<? extends T> actual)
Creates a new instance of
.IterableAssert
static AbstractLongAssert<?>
then(Long actual)
Creates a new instance of
.LongAssert
static AbstractShortAssert<?>
then(Short actual)
Creates a new instance of
.ShortAssert
static AbstractStringAssert<?>
then(String actual)
Creates a new instance of
.StringAssert
static AbstractCharSequenceAssert<?,? extends CharSequence>
then(StringBuffer actual)
Creates a new instance of
from aCharSequenceAssert
StringBuffer
.static AbstractCharSequenceAssert<?,? extends CharSequence>
then(StringBuilder actual)
Creates a new instance of
from aCharSequenceAssert
StringBuilder
.static AbstractThrowableAssert<?,? extends Throwable>
then(Throwable actual)
Creates a new instance of
.ThrowableAssert
static AbstractBigDecimalAssert<?>
then(BigDecimal actual)
Creates a new instance of
.BigDecimalAssert
static AbstractBigIntegerAssert<?>
then(BigInteger actual)
Creates a new instance of
.BigIntegerAssert
static AbstractUriAssert<?>
then(URI actual)
Creates a new instance of
.UriAssert
static AbstractUrlAssert<?>
then(URL actual)
Creates a new instance of
.UrlAssert
static AbstractPathAssert<?>
then(Path actual)
Creates a new instance ofPathAssert
static AbstractDurationAssert<?>
then(Duration actual)
Creates a new instance of
.DurationAssert
static AbstractInstantAssert<?>
then(Instant actual)
Creates a new instance of
.InstantAssert
static AbstractLocalDateAssert<?>
then(LocalDate actual)
Creates a new instance of
.LocalDateAssert
static AbstractLocalDateTimeAssert<?>
then(LocalDateTime actual)
Creates a new instance of
.LocalDateTimeAssert
static AbstractLocalTimeAssert<?>
then(LocalTime actual)
Creates a new instance of
.LocalTimeAssert
static AbstractOffsetDateTimeAssert<?>
then(OffsetDateTime actual)
Creates a new instance of
.OffsetTimeAssert
static AbstractOffsetTimeAssert<?>
then(OffsetTime actual)
Creates a new instance of
.OffsetTimeAssert
static AbstractPeriodAssert<?>
then(Period actual)
Creates a new instance of
.PeriodAssert
static AbstractZonedDateTimeAssert<?>
then(ZonedDateTime actual)
Creates a new instance of
.ZonedDateTimeAssert
static AtomicBooleanAssert
then(AtomicBoolean actual)
Create assertion forAtomicBoolean
.static AtomicIntegerAssert
then(AtomicInteger actual)
Create assertion forAtomicInteger
.static AtomicIntegerArrayAssert
then(AtomicIntegerArray actual)
Create assertion forAtomicIntegerArray
.static <OBJECT> AtomicIntegerFieldUpdaterAssert<OBJECT>
then(AtomicIntegerFieldUpdater<OBJECT> actual)
Create assertion forAtomicIntegerFieldUpdater
.static AtomicLongAssert
then(AtomicLong actual)
Create assertion forAtomicLong
.static AtomicLongArrayAssert
then(AtomicLongArray actual)
Create assertion forAtomicLongArray
.static <OBJECT> AtomicLongFieldUpdaterAssert<OBJECT>
then(AtomicLongFieldUpdater<OBJECT> actual)
Create assertion forAtomicLongFieldUpdater
.static <VALUE> AtomicMarkableReferenceAssert<VALUE>
then(AtomicMarkableReference<VALUE> actual)
Create assertion forAtomicMarkableReference
.static <VALUE> AtomicReferenceAssert<VALUE>
then(AtomicReference<VALUE> actual)
Create assertion forAtomicReference
.static <ELEMENT> AtomicReferenceArrayAssert<ELEMENT>
then(AtomicReferenceArray<ELEMENT> actual)
Create assertion forAtomicReferenceArray
.static <FIELD,OBJECT>
AtomicReferenceFieldUpdaterAssert<FIELD,OBJECT>then(AtomicReferenceFieldUpdater<OBJECT,FIELD> actual)
Create assertion forAtomicReferenceFieldUpdater
.static <VALUE> AtomicStampedReferenceAssert<VALUE>
then(AtomicStampedReference<VALUE> actual)
Create assertion forAtomicStampedReference
.static LongAdderAssert
then(LongAdder actual)
Create assertion forLongAdder
.static <RESULT> CompletableFutureAssert<RESULT>
then(CompletableFuture<RESULT> future)
Create assertion forCompletableFuture
.static <RESULT> CompletableFutureAssert<RESULT>
then(CompletionStage<RESULT> actual)
Create assertion forCompletionStage
by converting it to aCompletableFuture
and returning aCompletableFutureAssert
.static <RESULT> FutureAssert<RESULT>
then(Future<RESULT> actual)
Creates a new instance ofFutureAssert
static AbstractDateAssert<?>
then(Date actual)
Creates a new instance of
.DateAssert
static DoublePredicateAssert
then(DoublePredicate actual)
Create assertion forDoublePredicate
.static IntPredicateAssert
then(IntPredicate actual)
Create assertion forIntPredicate
.static LongPredicateAssert
then(LongPredicate actual)
Create assertion forLongPredicate
.static <T> PredicateAssert<T>
then(Predicate<T> actual)
Create assertion forPredicate
.static <T> IteratorAssert<T>
then(Iterator<? extends T> actual)
Creates a new instance of
.IteratorAssert
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)
Creates a new instance of
allowing to navigate to anyClassBasedNavigableListAssert
List
element in order to perform assertions on it.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)
Creates a new instance of
allowing to navigate to anyFactoryBasedNavigableListAssert
List
element in order to perform assertions on it.static <T> ListAssert<T>
then(List<? extends T> actual)
Creates a new instance of
.ListAssert
static <K,V>
MapAssert<K,V>then(Map<K,V> actual)
Creates a new instance of
.MapAssert
static <VALUE> OptionalAssert<VALUE>
then(Optional<VALUE> optional)
Create assertion forOptional
.static OptionalDoubleAssert
then(OptionalDouble optional)
Create assertion forOptionalDouble
.static OptionalIntAssert
then(OptionalInt optional)
Create assertion forOptionalInt
.static OptionalLongAssert
then(OptionalLong optional)
Create assertion forOptionalLong
.static <ELEMENT> SpliteratorAssert<ELEMENT>
then(Spliterator<ELEMENT> actual)
Creates a new instance of
from the givenSpliteratorAssert
Spliterator
.static ListAssert<Double>
then(DoubleStream actual)
Creates a new instance of
from the givenListAssert
DoubleStream
.static ListAssert<Integer>
then(IntStream actual)
Creates a new instance of
from the givenListAssert
IntStream
.static ListAssert<Long>
then(LongStream actual)
Creates a new instance of
from the givenListAssert
LongStream
.static <ELEMENT> ListAssert<ELEMENT>
then(Stream<? extends ELEMENT> actual)
Creates a new instance of
from the givenListAssert
Stream
.static <T> T
then(AssertProvider<T> component)
Delegates the creation of theAssert
to theAssertProvider.assertThat()
of the given component.static <T extends Comparable<? super T>>
AbstractComparableAssert<?,T>then(T actual)
Creates a new instance of
with standard comparison semantics.GenericComparableAssert
static <T> ObjectArrayAssert<T>
then(T[] actual)
Creates a new instance of
.ObjectArrayAssert
static <T> Object2DArrayAssert<T>
then(T[][] actual)
Creates a new instance of
.Object2DArrayAssert
static AbstractThrowableAssert<?,? extends Throwable>
thenCode(ThrowableAssert.ThrowingCallable shouldRaiseOrNotThrowable)
Allows to capture and then assert on aThrowable
more easily when used with Java 8 lambdas.static <T extends Throwable>
ThrowableTypeAssert<T>thenExceptionOfType(Class<? extends T> exceptionType)
Entry point to check that an exception of type T is thrown by a giventhrowingCallable
which allows to chain assertions on the thrown exception.static ThrowableTypeAssert<IllegalArgumentException>
thenIllegalArgumentException()
Alias forthenExceptionOfType(Class)
forIllegalArgumentException
.static ThrowableTypeAssert<IllegalStateException>
thenIllegalStateException()
Alias forthenExceptionOfType(Class)
forIllegalStateException
.static ThrowableTypeAssert<IOException>
thenIOException()
Alias forthenExceptionOfType(Class)
forIOException
.static NotThrownAssert
thenNoException()
Entry point to check that no exception of any type is thrown by a giventhrowingCallable
.static ThrowableTypeAssert<NullPointerException>
thenNullPointerException()
Alias forthenExceptionOfType(Class)
forNullPointerException
.static <T> ObjectAssert<T>
thenObject(T actual)
Creates a new instance of
for any object.ObjectAssert
static AbstractThrowableAssert<?,? extends Throwable>
thenThrownBy(ThrowableAssert.ThrowingCallable shouldRaiseThrowable)
Allows to capture and then assert on aThrowable
(easier done with lambdas).static AbstractThrowableAssert<?,? extends Throwable>
thenThrownBy(ThrowableAssert.ThrowingCallable shouldRaiseThrowable, String description, Object... args)
Allows to capture and then assert on aThrowable
likethenThrownBy(ThrowingCallable)
but this method let you set the assertion description the same way you do withas(String, Object...)
.-
Methods inherited from class org.assertj.core.api.Assertions
allOf, allOf, anyOf, anyOf, as, 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, assertThat, assertThat, assertThat, assertThat, assertThat, assertThat, assertThat, assertThat, assertThat, assertThat, assertThat, assertThatCode, assertThatExceptionOfType, assertThatIllegalArgumentException, assertThatIllegalStateException, assertThatIOException, assertThatNoException, 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, setDescriptionConsumer, setExtractBareNamePropertyMethods, setLenientDateParsing, setMaxElementsForPrinting, setMaxLengthForSingleLineDescription, setMaxStackTraceElementsDisplayed, setPrintAssertionsDescription, setRemoveAssertJRelatedElementsFromStackTrace, shouldHaveThrown, tuple, useDefaultDateFormatsOnly, useDefaultRepresentation, useRepresentation, within, within, within, within, within, within, within, within, within, withinPercentage, withinPercentage, withinPercentage, withMarginOf, withPrecision, withPrecision
-
-
-
-
Field Detail
-
and
public static final BDDAssertions and
ABDDAssertions
which allows to blend assertions with other libraries when the name 'then
' cause clash.Examples:
import static org.assertj.core.api.BDDAssertions.and; import static org.mockito.BDDMockito.then; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.times; // suppress and.then warning: The static method BDDAssertions.then() should be accessed in a static way @SuppressWarnings("static-access") @Test public void bdd_assertions_with_bdd_mockito() { // GIVEN Person person = mock(Person.class) // WHEN person.ride(bike); person.ride(bike); // THEN // mockito then() then(person).should(times(2)).ride(bike); // use AssertJ and.then(person) as then(person) would clash with mockito then(person) and.then(person.hasBike()).isTrue(); }
Can also be used to add extra readability:
import static org.assertj.core.api.BDDAssertions.and; import static org.assertj.core.api.BDDAssertions.then; // suppress and.then warning: The static method BDDAssertions.then() should be accessed in a static way @SuppressWarnings("static-access") @Test public void bdd_assertions_with_and_field() { // ... then(person.hasBike()).isTrue() and.then(bike.isNew()).isFalse(); }
- Since:
- 3.14.0
-
-
Constructor Detail
-
BDDAssertions
protected BDDAssertions()
Creates a new
.BDDAssertions
-
-
Method Detail
-
then
public static <T> PredicateAssert<T> then(Predicate<T> actual)
Create assertion forPredicate
.- Type Parameters:
T
- the type of the value contained in thePredicate
.- Parameters:
actual
- the actual value.- Returns:
- the created assertion object.
- Since:
- 3.5.0
-
then
public static IntPredicateAssert then(IntPredicate actual)
Create assertion forIntPredicate
.- Parameters:
actual
- the actual value.- Returns:
- the created assertion object.
- Since:
- 3.5.0
-
then
public static LongPredicateAssert then(LongPredicate actual)
Create assertion forLongPredicate
.- Parameters:
actual
- the actual value.- Returns:
- the created assertion object.
- Since:
- 3.5.0
-
then
public static DoublePredicateAssert then(DoublePredicate actual)
Create assertion forDoublePredicate
.- Parameters:
actual
- the actual value.- Returns:
- the created assertion object.
- Since:
- 3.5.0
-
then
public static <VALUE> OptionalAssert<VALUE> then(Optional<VALUE> optional)
Create assertion forOptional
.- Type Parameters:
VALUE
- the type of the value contained in theOptional
.- Parameters:
optional
- the actual value.- Returns:
- the created assertion object.
-
then
public static OptionalIntAssert then(OptionalInt optional)
Create assertion forOptionalInt
.- Parameters:
optional
- the actual value.- Returns:
- the created assertion object.
-
then
public static OptionalLongAssert then(OptionalLong optional)
Create assertion forOptionalLong
.- Parameters:
optional
- the actual value.- Returns:
- the created assertion object.
-
then
public static OptionalDoubleAssert then(OptionalDouble optional)
Create assertion forOptionalDouble
.- Parameters:
optional
- the actual value.- Returns:
- the created assertion object.
-
then
public static AbstractBigDecimalAssert<?> then(BigDecimal actual)
Creates a new instance of
.BigDecimalAssert
- Parameters:
actual
- the actual value.- Returns:
- the created assertion object.
-
then
public static AbstractBigIntegerAssert<?> then(BigInteger actual)
Creates a new instance of
.BigIntegerAssert
- Parameters:
actual
- the actual value.- Returns:
- the created assertion object.
- Since:
- 2.7.0 / 3.7.0
-
then
public static AbstractBooleanAssert<?> then(boolean actual)
Creates a new instance of
.BooleanAssert
- Parameters:
actual
- the actual value.- Returns:
- the created assertion object.
-
then
public static AbstractBooleanAssert<?> then(Boolean actual)
Creates a new instance of
.BooleanAssert
- Parameters:
actual
- the actual value.- Returns:
- the created assertion object.
-
then
public static AbstractBooleanArrayAssert<?> then(boolean[] actual)
Creates a new instance of
.BooleanArrayAssert
- Parameters:
actual
- the actual value.- Returns:
- the created assertion object.
-
then
public static Boolean2DArrayAssert then(boolean[][] actual)
Creates a new instance of
.Boolean2DArrayAssert
- Parameters:
actual
- the actual value.- Returns:
- the created assertion object.
- Since:
- 3.17.0
-
then
public static AbstractByteAssert<?> then(byte actual)
Creates a new instance of
.ByteAssert
- Parameters:
actual
- the actual value.- Returns:
- the created assertion object.
-
then
public static AbstractByteAssert<?> then(Byte actual)
Creates a new instance of
.ByteAssert
- Parameters:
actual
- the actual value.- Returns:
- the created assertion object.
-
then
public static AbstractByteArrayAssert<?> then(byte[] actual)
Creates a new instance of
.ByteArrayAssert
- Parameters:
actual
- the actual value.- Returns:
- the created assertion object.
-
then
public static Byte2DArrayAssert then(byte[][] actual)
Creates a new instance of
.Byte2DArrayAssert
- Parameters:
actual
- the actual value.- Returns:
- the created assertion object.
- Since:
- 3.17.0
-
then
public static AbstractCharacterAssert<?> then(char actual)
Creates a new instance of
.CharacterAssert
- Parameters:
actual
- the actual value.- Returns:
- the created assertion object.
-
then
public static AbstractCharArrayAssert<?> then(char[] actual)
Creates a new instance of
.CharArrayAssert
- Parameters:
actual
- the actual value.- Returns:
- the created assertion object.
-
then
public static Char2DArrayAssert then(char[][] actual)
Creates a new instance of
.Char2DArrayAssert
- Parameters:
actual
- the actual value.- Returns:
- the created assertion object.
- Since:
- 3.17.0
-
then
public static AbstractCharacterAssert<?> then(Character actual)
Creates a new instance of
.CharacterAssert
- Parameters:
actual
- the actual value.- Returns:
- the created assertion object.
-
then
public static ClassAssert then(Class<?> actual)
Creates a new instance ofClassAssert
- Parameters:
actual
- the actual value.- Returns:
- the created assertion object.
-
then
public static <T extends Comparable<? super T>> AbstractComparableAssert<?,T> then(T actual)
Creates a new instance of
with standard comparison semantics.GenericComparableAssert
- Type Parameters:
T
- the actual type- Parameters:
actual
- the actual value.- Returns:
- the created assertion object.
-
then
public static <T> IterableAssert<T> then(Iterable<? extends T> actual)
Creates a new instance of
.IterableAssert
- Type Parameters:
T
- the actual elements type- Parameters:
actual
- the actual value.- Returns:
- the created assertion object.
-
then
public static <T> IteratorAssert<T> then(Iterator<? extends T> actual)
Creates a new instance of
.IteratorAssert
Breaking change in version 3.12.0: this method does not return anymore an
IterableAssert
but anIteratorAssert
.
In order to access assertions fromIterableAssert
, useAbstractIteratorAssert.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
- Type Parameters:
T
- the actual elements type- Parameters:
actual
- the actual value.- Returns:
- the created assertion object.
-
then
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)
Creates a new instance of
allowing to navigate to anyFactoryBasedNavigableIterableAssert
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 givenAssertFactory<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");
- Type Parameters:
ACTUAL
- The actual typeELEMENT
- The actual elements typeELEMENT_ASSERT
- The actual elements AbstractAssert type- Parameters:
actual
- the actual value.assertFactory
- the factory used to create the elements assert instance.- Returns:
- the created assertion object.
-
then
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)
Creates a new instance of
allowing to navigate to anyClassBasedNavigableIterableAssert
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");
- Type Parameters:
ACTUAL
- The actual typeELEMENT
- The actual elements typeELEMENT_ASSERT
- The actual elements AbstractAssert type- Parameters:
actual
- the actual value.assertClass
- the class used to create the elements assert instance.- Returns:
- the created assertion object.
-
then
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)
Creates a new instance of
allowing to navigate to anyFactoryBasedNavigableListAssert
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 givenAssertFactory<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");
- Type Parameters:
ACTUAL
- The actual typeELEMENT
- The actual elements typeELEMENT_ASSERT
- The actual elements AbstractAssert type- Parameters:
actual
- the actual value.assertFactory
- the factory used to create the elements assert instance.- Returns:
- the created assertion object.
-
then
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)
Creates a new instance of
allowing to navigate to anyClassBasedNavigableListAssert
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");
- Type Parameters:
ACTUAL
- The actual typeELEMENT
- The actual elements typeELEMENT_ASSERT
- The actual elements AbstractAssert type- Parameters:
actual
- the actual value.assertClass
- the class used to create the elements assert instance.- Returns:
- the created assertion object.
-
then
public static AbstractDoubleAssert<?> then(double actual)
Creates a new instance of
.DoubleAssert
- Parameters:
actual
- the actual value.- Returns:
- the created assertion object.
-
then
public static AbstractDoubleAssert<?> then(Double actual)
Creates a new instance of
.DoubleAssert
- Parameters:
actual
- the actual value.- Returns:
- the created assertion object.
-
then
public static AbstractDoubleArrayAssert<?> then(double[] actual)
Creates a new instance of
.DoubleArrayAssert
- Parameters:
actual
- the actual value.- Returns:
- the created assertion object.
-
then
public static Double2DArrayAssert then(double[][] actual)
Creates a new instance of
.Double2DArrayAssert
- Parameters:
actual
- the actual value.- Returns:
- the created assertion object.
- Since:
- 3.17.0
-
then
public static AbstractFileAssert<?> then(File actual)
Creates a new instance of
.FileAssert
- Parameters:
actual
- the actual value.- Returns:
- the created assertion object.
-
then
public static AbstractPathAssert<?> then(Path actual)
Creates a new instance ofPathAssert
- Parameters:
actual
- the path to test- Returns:
- the created assertion object
-
then
public static <RESULT> FutureAssert<RESULT> then(Future<RESULT> actual)
Creates a new instance ofFutureAssert
- Type Parameters:
RESULT
- the type of the value contained in theFuture
.- Parameters:
actual
- the future to test- Returns:
- the created assertion object
- Since:
- 2.7.0 / 3.7.0
-
then
public static AbstractInputStreamAssert<?,? extends InputStream> then(InputStream actual)
Creates a new instance of
.InputStreamAssert
- Parameters:
actual
- the actual value.- Returns:
- the created assertion object.
-
then
public static AbstractFloatAssert<?> then(float actual)
Creates a new instance of
.FloatAssert
- Parameters:
actual
- the actual value.- Returns:
- the created assertion object.
-
then
public static AbstractFloatAssert<?> then(Float actual)
Creates a new instance of
.FloatAssert
- Parameters:
actual
- the actual value.- Returns:
- the created assertion object.
-
then
public static AbstractFloatArrayAssert<?> then(float[] actual)
Creates a new instance of
.FloatArrayAssert
- Parameters:
actual
- the actual value.- Returns:
- the created assertion object.
-
then
public static Float2DArrayAssert then(float[][] actual)
Creates a new instance of
.Float2DArrayAssert
- Parameters:
actual
- the actual value.- Returns:
- the created assertion object.
- Since:
- 3.17.0
-
then
public static AbstractIntegerAssert<?> then(int actual)
Creates a new instance of
.IntegerAssert
- Parameters:
actual
- the actual value.- Returns:
- the created assertion object.
-
then
public static AbstractIntArrayAssert<?> then(int[] actual)
Creates a new instance of
.IntArrayAssert
- Parameters:
actual
- the actual value.- Returns:
- the created assertion object.
-
then
public static Int2DArrayAssert then(int[][] actual)
Creates a new instance of
.Int2DArrayAssert
- Parameters:
actual
- the actual value.- Returns:
- the created assertion object.
- Since:
- 3.17.0
-
then
public static AbstractIntegerAssert<?> then(Integer actual)
Creates a new instance of
.IntegerAssert
- Parameters:
actual
- the actual value.- Returns:
- the created assertion object.
-
then
public static <T> ListAssert<T> then(List<? extends T> actual)
Creates a new instance of
.ListAssert
- Type Parameters:
T
- the type of elements.- Parameters:
actual
- the actual value.- Returns:
- the created assertion object.
-
then
public static AbstractLongAssert<?> then(long actual)
Creates a new instance of
.LongAssert
- Parameters:
actual
- the actual value.- Returns:
- the created assertion object.
-
then
public static AbstractLongAssert<?> then(Long actual)
Creates a new instance of
.LongAssert
- Parameters:
actual
- the actual value.- Returns:
- the created assertion object.
-
then
public static AbstractLongArrayAssert<?> then(long[] actual)
Creates a new instance of
.LongArrayAssert
- Parameters:
actual
- the actual value.- Returns:
- the created assertion object.
-
then
public static Long2DArrayAssert then(long[][] actual)
Creates a new instance of
.Long2DArrayAssert
- Parameters:
actual
- the actual value.- Returns:
- the created assertion object.
- Since:
- 3.17.0
-
then
public static <T> ObjectAssert<T> then(T actual)
Creates a new instance of
.ObjectAssert
- Type Parameters:
T
- the type of the actual value.- Parameters:
actual
- the actual value.- Returns:
- the created assertion object.
-
then
public static <T> ObjectArrayAssert<T> then(T[] actual)
Creates a new instance of
.ObjectArrayAssert
- Type Parameters:
T
- the actual's elements type.- Parameters:
actual
- the actual value.- Returns:
- the created assertion object.
-
then
public static <T> Object2DArrayAssert<T> then(T[][] actual)
Creates a new instance of
.Object2DArrayAssert
- Type Parameters:
T
- the actual's elements type.- Parameters:
actual
- the actual value.- Returns:
- the created assertion object.
- Since:
- 3.17.0
-
then
public static <K,V> MapAssert<K,V> then(Map<K,V> actual)
Creates a new instance of
.MapAssert
- Type Parameters:
K
- the type of keys in the map.V
- the type of values in the map.- Parameters:
actual
- the actual value.- Returns:
- the created assertion object.
-
then
public static AbstractShortAssert<?> then(short actual)
Creates a new instance of
.ShortAssert
- Parameters:
actual
- the actual value.- Returns:
- the created assertion object.
-
then
public static AbstractShortAssert<?> then(Short actual)
Creates a new instance of
.ShortAssert
- Parameters:
actual
- the actual value.- Returns:
- the created assertion object.
-
then
public static AbstractShortArrayAssert<?> then(short[] actual)
Creates a new instance of
.ShortArrayAssert
- Parameters:
actual
- the actual value.- Returns:
- the created assertion object.
-
then
public static Short2DArrayAssert then(short[][] actual)
Creates a new instance of
.Short2DArrayAssert
- Parameters:
actual
- the actual value.- Returns:
- the created assertion object.
- Since:
- 3.17.0
-
then
public static AbstractCharSequenceAssert<?,? extends CharSequence> then(CharSequence actual)
Creates a new instance of
.CharSequenceAssert
- Parameters:
actual
- the actual value.- Returns:
- the created assertion object.
-
then
public static AbstractCharSequenceAssert<?,? extends CharSequence> then(StringBuilder actual)
Creates a new instance of
from aCharSequenceAssert
StringBuilder
.- Parameters:
actual
- the actual value.- Returns:
- the created assertion object.
- Since:
- 3.11.0
-
then
public static AbstractCharSequenceAssert<?,? extends CharSequence> then(StringBuffer actual)
Creates a new instance of
from aCharSequenceAssert
StringBuffer
.- Parameters:
actual
- the actual value.- Returns:
- the created assertion object.
- Since:
- 3.11.0
-
then
public static AbstractStringAssert<?> then(String actual)
Creates a new instance of
.StringAssert
- Parameters:
actual
- the actual value.- Returns:
- the created assertion object.
-
then
public static AbstractDateAssert<?> then(Date actual)
Creates a new instance of
.DateAssert
- Parameters:
actual
- the actual value.- Returns:
- the created assertion object.
-
then
public static AtomicBooleanAssert then(AtomicBoolean actual)
Create assertion forAtomicBoolean
.- Parameters:
actual
- the actual value.- Returns:
- the created assertion object.
- Since:
- 2.7.0 / 3.7.0
-
then
public static AtomicIntegerAssert then(AtomicInteger actual)
Create assertion forAtomicInteger
.- Parameters:
actual
- the actual value.- Returns:
- the created assertion object.
- Since:
- 2.7.0 / 3.7.0
-
then
public static AtomicIntegerArrayAssert then(AtomicIntegerArray actual)
Create assertion forAtomicIntegerArray
.- Parameters:
actual
- the actual value.- Returns:
- the created assertion object.
- Since:
- 2.7.0 / 3.7.0
-
then
public static <OBJECT> AtomicIntegerFieldUpdaterAssert<OBJECT> then(AtomicIntegerFieldUpdater<OBJECT> actual)
Create assertion forAtomicIntegerFieldUpdater
.- Type Parameters:
OBJECT
- the type of the object holding the updatable field.- Parameters:
actual
- the actual value.- Returns:
- the created assertion object.
- Since:
- 2.7.0 / 3.7.0
-
then
public static LongAdderAssert then(LongAdder actual)
Create assertion forLongAdder
.- Parameters:
actual
- the actual value.- Returns:
- the created assertion object.
- Since:
- 3.16.0
-
then
public static AtomicLongAssert then(AtomicLong actual)
Create assertion forAtomicLong
.- Parameters:
actual
- the actual value.- Returns:
- the created assertion object.
- Since:
- 2.7.0 / 3.7.0
-
then
public static AtomicLongArrayAssert then(AtomicLongArray actual)
Create assertion forAtomicLongArray
.- Parameters:
actual
- the actual value.- Returns:
- the created assertion object.
- Since:
- 2.7.0 / 3.7.0
-
then
public static <OBJECT> AtomicLongFieldUpdaterAssert<OBJECT> then(AtomicLongFieldUpdater<OBJECT> actual)
Create assertion forAtomicLongFieldUpdater
.- Type Parameters:
OBJECT
- the type of the object holding the updatable field.- Parameters:
actual
- the actual value.- Returns:
- the created assertion object.
- Since:
- 2.7.0 / 3.7.0
-
then
public static <VALUE> AtomicReferenceAssert<VALUE> then(AtomicReference<VALUE> actual)
Create assertion forAtomicReference
.- Type Parameters:
VALUE
- the type of the value contained in theAtomicReference
.- Parameters:
actual
- the actual value.- Returns:
- the created assertion object.
- Since:
- 2.7.0 / 3.7.0
-
then
public static <ELEMENT> AtomicReferenceArrayAssert<ELEMENT> then(AtomicReferenceArray<ELEMENT> actual)
Create assertion forAtomicReferenceArray
.- Type Parameters:
ELEMENT
- the type of the value contained in theAtomicReferenceArray
.- Parameters:
actual
- the actual value.- Returns:
- the created assertion object.
- Since:
- 2.7.0 / 3.7.0
-
then
public static <FIELD,OBJECT> AtomicReferenceFieldUpdaterAssert<FIELD,OBJECT> then(AtomicReferenceFieldUpdater<OBJECT,FIELD> actual)
Create assertion forAtomicReferenceFieldUpdater
.- Type Parameters:
FIELD
- the type of the field which gets updated by theAtomicReferenceFieldUpdater
.OBJECT
- the type of the object holding the updatable field.- Parameters:
actual
- the actual value.- Returns:
- the created assertion object.
- Since:
- 2.7.0 / 3.7.0
-
then
public static <VALUE> AtomicMarkableReferenceAssert<VALUE> then(AtomicMarkableReference<VALUE> actual)
Create assertion forAtomicMarkableReference
.- Type Parameters:
VALUE
- the type of the value contained in theAtomicMarkableReference
.- Parameters:
actual
- the actual value.- Returns:
- the created assertion object.
- Since:
- 2.7.0 / 3.7.0
-
then
public static <VALUE> AtomicStampedReferenceAssert<VALUE> then(AtomicStampedReference<VALUE> actual)
Create assertion forAtomicStampedReference
.- Type Parameters:
VALUE
- the type of the value contained in theAtomicStampedReference
.- Parameters:
actual
- the actual value.- Returns:
- the created assertion object.
- Since:
- 2.7.0 / 3.7.0
-
then
public static AbstractThrowableAssert<?,? extends Throwable> then(Throwable actual)
Creates a new instance of
.ThrowableAssert
- Parameters:
actual
- the actual value.- Returns:
- the created assertion Throwable.
-
thenThrownBy
public static AbstractThrowableAssert<?,? extends Throwable> thenThrownBy(ThrowableAssert.ThrowingCallable shouldRaiseThrowable)
Allows to capture and then assert on aThrowable
(easier done with lambdas).Example :
If the provided@Test public void testException() { thenThrownBy(() -> { throw new Exception("boom!") }).isInstanceOf(Exception.class) .hasMessageContaining("boom"); }
ThrowableAssert.ThrowingCallable
does not raise an exception, an error is immediately thrown, in that case the test description provided withas(String, Object...)
is not honored.
To use a test description, useAssertions.catchThrowable(ThrowableAssert.ThrowingCallable)
as shown below:
Alternatively you can also use// 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);
assertThatCode(ThrowingCallable)
for the test description provided withas(String, Object...)
to always be honored.- Parameters:
shouldRaiseThrowable
- TheThrowableAssert.ThrowingCallable
or lambda with the code that should raise the throwable.- Returns:
- the created
ThrowableAssert
.
-
thenThrownBy
public static AbstractThrowableAssert<?,? extends Throwable> thenThrownBy(ThrowableAssert.ThrowingCallable shouldRaiseThrowable, String description, Object... args)
Allows to capture and then assert on aThrowable
likethenThrownBy(ThrowingCallable)
but this method let you set the assertion description the same way you do withas(String, Object...)
.Example:
If the provided@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"); }
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);
- Parameters:
shouldRaiseThrowable
- TheThrowableAssert.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.- Returns:
- the created
ThrowableAssert
. - Since:
- 3.9.0
-
thenCode
public static AbstractThrowableAssert<?,? extends Throwable> thenCode(ThrowableAssert.ThrowingCallable shouldRaiseOrNotThrowable)
Allows to capture and then assert on aThrowable
more easily when used with Java 8 lambdas.Example :
If the providedThrowingCallable callable = () -> { throw new Exception("boom!"); }; // assertion succeeds thenCode(callable).isInstanceOf(Exception.class) .hasMessageContaining("boom"); // assertion fails thenCode(callable).doesNotThrowAnyException();
ThrowableAssert.ThrowingCallable
does not validate against next assertions, an error is immediately raised, in that case the test description provided withas(String, Object...)
is not honored.
To use a test description, useAssertions.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 :(- Parameters:
shouldRaiseOrNotThrowable
- TheThrowableAssert.ThrowingCallable
or lambda with the code that should raise the throwable.- Returns:
- The captured exception or
null
if none was raised by the callable. - Since:
- 3.7.0
-
thenObject
public static <T> ObjectAssert<T> thenObject(T actual)
Creates a new instance of
for any object.ObjectAssert
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:
No cast needed, but also no additional list assertions:then(new LinkedList<>(asList("abc"))).matches(list -> ((Deque<String>) list).getFirst().equals("abc"));
thenObject(new LinkedList<>(asList("abc"))).matches(list -> list.getFirst().equals("abc"));
- Type Parameters:
T
- the type of the actual value.- Parameters:
actual
- the actual value.- Returns:
- the created assertion object.
- Since:
- 3.12.0
-
then
public static AbstractLocalDateAssert<?> then(LocalDate actual)
Creates a new instance of
.LocalDateAssert
- Parameters:
actual
- the actual value.- Returns:
- the created assertion object.
-
then
public static AbstractLocalDateTimeAssert<?> then(LocalDateTime actual)
Creates a new instance of
.LocalDateTimeAssert
- Parameters:
actual
- the actual value.- Returns:
- the created assertion object.
-
then
public static AbstractZonedDateTimeAssert<?> then(ZonedDateTime actual)
Creates a new instance of
.ZonedDateTimeAssert
- Parameters:
actual
- the actual value.- Returns:
- the created assertion object.
-
then
public static AbstractLocalTimeAssert<?> then(LocalTime actual)
Creates a new instance of
.LocalTimeAssert
- Parameters:
actual
- the actual value.- Returns:
- the created assertion object.
-
then
public static AbstractOffsetTimeAssert<?> then(OffsetTime actual)
Creates a new instance of
.OffsetTimeAssert
- Parameters:
actual
- the actual value.- Returns:
- the created assertion object.
-
then
public static AbstractInstantAssert<?> then(Instant actual)
Creates a new instance of
.InstantAssert
- Parameters:
actual
- the actual value.- Returns:
- the created assertion object.
- Since:
- 3.7.0
-
then
public static AbstractDurationAssert<?> then(Duration actual)
Creates a new instance of
.DurationAssert
- Parameters:
actual
- the actual value.- Returns:
- the created assertion object.
- Since:
- 3.15.0
-
then
public static AbstractPeriodAssert<?> then(Period actual)
Creates a new instance of
.PeriodAssert
- Parameters:
actual
- the actual value.- Returns:
- the created assertion object.
- Since:
- 3.17.0
-
then
public static AbstractUriAssert<?> then(URI actual)
Creates a new instance of
.UriAssert
- Parameters:
actual
- the actual value.- Returns:
- the created assertion object.
-
then
public static AbstractUrlAssert<?> then(URL actual)
Creates a new instance of
.UrlAssert
- Parameters:
actual
- the actual value.- Returns:
- the created assertion object.
-
then
public static AbstractOffsetDateTimeAssert<?> then(OffsetDateTime actual)
Creates a new instance of
.OffsetTimeAssert
- Parameters:
actual
- the actual value.- Returns:
- the created assertion object.
-
then
public static <RESULT> CompletableFutureAssert<RESULT> then(CompletableFuture<RESULT> future)
Create assertion forCompletableFuture
.- Type Parameters:
RESULT
- the type of the value contained in theCompletableFuture
.- Parameters:
future
- the actual value.- Returns:
- the created assertion object.
-
then
public static <RESULT> CompletableFutureAssert<RESULT> then(CompletionStage<RESULT> actual)
Create assertion forCompletionStage
by converting it to aCompletableFuture
and returning aCompletableFutureAssert
.If the given
CompletionStage
is null, theCompletableFuture
in the returnedCompletableFutureAssert
will also be null.- Type Parameters:
RESULT
- the type of the value contained in theCompletionStage
.- Parameters:
actual
- the actual value.- Returns:
- the created assertion object.
-
then
public static <T extends AssertDelegateTarget> T then(T assertion)
Returns the given assertion. This method improves code readability by surrounding the given assertion withthen
.Consider for example the following MyButton and MyButtonAssert classes:
As MyButtonAssert implements AssertDelegateTarget, you can usepublic 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(); } }
then(buttonAssert).isBlinking();
instead ofbuttonAssert.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(); }
- Type Parameters:
T
- the generic type of the user-defined assert.- Parameters:
assertion
- the assertion to return.- Returns:
- the given assertion.
-
then
public static <T> T then(AssertProvider<T> component)
Delegates the creation of theAssert
to theAssertProvider.assertThat()
of the given component.Read the comments on
AssertProvider
for an example of its usage.- Type Parameters:
T
- the AssertProvider wrapped type.- Parameters:
component
- the component that creates its own assert- Returns:
- the associated
Assert
of the given component
-
then
public static <ELEMENT> ListAssert<ELEMENT> then(Stream<? extends ELEMENT> actual)
Creates a new instance of
from the givenListAssert
Stream
.Be aware that the
Stream
under test will be converted to aList
when an assertions require to inspect its content. Once this is done theStream
can't reused as it would have been consumed.Calling multiple methods on the returned
ListAssert
is safe as it only interacts with theList
built from theStream
.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
andisSameAs
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);
- Type Parameters:
ELEMENT
- the type of elements.- Parameters:
actual
- the actualStream
value.- Returns:
- the created assertion object.
-
then
public static ListAssert<Double> then(DoubleStream actual)
Creates a new instance of
from the givenListAssert
DoubleStream
.Be aware that the
DoubleStream
under test will be converted to aList
when an assertions require to inspect its content. Once this is done theDoubleStream
can't reused as it would have been consumed.Calling multiple methods on the returned
ListAssert
is safe as it only interacts with theList
built from theDoubleStream
.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
andisSameAs
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);
- Parameters:
actual
- the actualDoubleStream
value.- Returns:
- the created assertion object.
-
then
public static ListAssert<Long> then(LongStream actual)
Creates a new instance of
from the givenListAssert
LongStream
.Be aware that the
LongStream
under test will be converted to aList
when an assertions require to inspect its content. Once this is done theLongStream
can't reused as it would have been consumed.Calling multiple methods on the returned
ListAssert
is safe as it only interacts with theList
built from theLongStream
.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
andisSameAs
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);
- Parameters:
actual
- the actualLongStream
value.- Returns:
- the created assertion object.
-
then
public static ListAssert<Integer> then(IntStream actual)
Creates a new instance of
from the givenListAssert
IntStream
.Be aware that the
IntStream
under test will be converted to aList
when an assertions require to inspect its content. Once this is done theIntStream
can't reused as it would have been consumed.Calling multiple methods on the returned
ListAssert
is safe as it only interacts with theList
built from theIntStream
.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
andisSameAs
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);
- Parameters:
actual
- the actualIntStream
value.- Returns:
- the created assertion object.
-
then
public static <ELEMENT> SpliteratorAssert<ELEMENT> then(Spliterator<ELEMENT> actual)
Creates a new instance of
from the givenSpliteratorAssert
Spliterator
. Example:Spliterator<Integer> spliterator = Stream.of(1, 2, 3).spliterator(); then(spliterator).hasCharacteristics(Spliterator.SIZED);
- Type Parameters:
ELEMENT
- the type of elements.- Parameters:
actual
- the spliterator to test.- Returns:
- the created assertion object.
- Since:
- 3.14.0
-
thenExceptionOfType
public static <T extends Throwable> ThrowableTypeAssert<T> thenExceptionOfType(Class<? extends T> exceptionType)
Entry point to check that an exception of type T is thrown by a giventhrowingCallable
which allows to chain assertions on the thrown exception.Example:
This method is more or less the same ofthenExceptionOfType(IOException.class) .isThrownBy(() -> { throw new IOException("boom!"); }) .withMessage("boom!");
thenThrownBy(ThrowableAssert.ThrowingCallable)
but in a more natural way.- Type Parameters:
T
- the exception type.- Parameters:
exceptionType
- the exception type class.- Returns:
- the created
ThrowableTypeAssert
.
-
thenNoException
public static NotThrownAssert thenNoException()
Entry point to check that no exception of any type is thrown by a giventhrowingCallable
.Example:
This method is more or less the same ofthenNoException().isThrownBy(() -> { System.out.println("OK"); });
thenCode(...).doesNotThrowAnyException();
but in a more natural way.- Returns:
- the created
NotThrownAssert
. - Since:
- 3.17.0
-
thenNullPointerException
public static ThrowableTypeAssert<NullPointerException> thenNullPointerException()
Alias forthenExceptionOfType(Class)
forNullPointerException
.- Returns:
- the created
ThrowableTypeAssert
.
-
thenIllegalArgumentException
public static ThrowableTypeAssert<IllegalArgumentException> thenIllegalArgumentException()
Alias forthenExceptionOfType(Class)
forIllegalArgumentException
.- Returns:
- the created
ThrowableTypeAssert
.
-
thenIOException
public static ThrowableTypeAssert<IOException> thenIOException()
Alias forthenExceptionOfType(Class)
forIOException
.- Returns:
- the created
ThrowableTypeAssert
.
-
thenIllegalStateException
public static ThrowableTypeAssert<IllegalStateException> thenIllegalStateException()
Alias forthenExceptionOfType(Class)
forIllegalStateException
.- Returns:
- the created
ThrowableTypeAssert
.
-
-