public class Java6BDDAssertions extends Object
BDDAssertions
.BDDAssertions
Modifier | Constructor and Description |
---|---|
protected |
Java6BDDAssertions()
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 int[] 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 AbstractClassAssert<?> |
then(Class<?> actual)
Creates a new instance of
|
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 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> AbstractFutureAssert<?,? extends Future<? extends RESULT>,RESULT> |
then(Future<RESULT> actual)
Creates a new instance of
. |
static AbstractInputStreamAssert<?,? extends InputStream> |
then(InputStream 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 <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> AbstractIterableAssert<?,Iterable<? extends T>,T,ObjectAssert<T>> |
then(Iterable<? extends T> actual)
Creates a new instance of
. |
static <T> AbstractIteratorAssert<?,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
allowing to navigate to any List element
in order to perform assertions on it. |
static <T> AbstractListAssert<?,List<? extends T>,T,ObjectAssert<T>> |
then(List<? extends T> 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 <K,V> MapAssert<K,V> |
then(Map<K,V> actual)
Creates a new instance of
. |
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 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> AbstractObjectAssert<?,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> AbstractObjectArrayAssert<?,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
. |
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> AbstractObjectAssert<?,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 more easily when used with Java 8 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...) . |
protected Java6BDDAssertions()
BDDAssertions
.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 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 AbstractClassAssert<?> 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> AbstractIterableAssert<?,Iterable<? extends T>,T,ObjectAssert<T>> then(Iterable<? extends T> actual)
IterableAssert
.T
- the actual elements typeactual
- the actual value.public static <T> AbstractIteratorAssert<?,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
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 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 <RESULT> AbstractFutureAssert<?,? extends Future<? extends RESULT>,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> AbstractListAssert<?,List<? extends T>,T,ObjectAssert<T>> then(List<? extends T> actual)
ListAssert
.T
- the actual elements typeactual
- 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> AbstractObjectAssert<?,T> then(T actual)
ObjectAssert
.T
- the actual typeactual
- the actual value.public static <T> AbstractObjectArrayAssert<?,T> then(T[] actual)
ObjectArrayAssert
.T
- the actual elements typeactual
- 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 AbstractThrowableAssert<?,? extends Throwable> then(Throwable actual)
ThrowableAssert
.actual
- the actual value.public static AbstractThrowableAssert<?,? extends Throwable> thenThrownBy(ThrowableAssert.ThrowingCallable shouldRaiseThrowable)
Throwable
more easily when used with Java 8 lambdas.
Java 8 example :
@Test
public void testException() {
thenThrownBy(() -> { throw new Exception("boom!") }).isInstanceOf(Exception.class)
.hasMessageContaining("boom");
}
Java 7 example :
thenThrownBy(new ThrowingCallable() {
@Override
public Void call() throws Exception {
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(() -> {});
then(thrown).as("display me")
.isInstanceOf(Exception.class);
Alternatively you can also use thenCode(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.null
if none was raised by the callable.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 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();
Contrary to thenThrownBy(ThrowingCallable)
the test description provided with
as(String, Object...)
is always honored 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);
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 AbstractUriAssert<?> then(URI actual)
UriAssert
.actual
- the actual value.public static AbstractUrlAssert<?> then(URL actual)
UrlAssert
.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 <T> AbstractObjectAssert<?,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.Copyright © 2014–2019 AssertJ. All rights reserved.