public class AssertionsForInterfaceTypes extends AssertionsForClassTypes
For example:
int removed = employees.removeFired();
assertThat
(removed).isZero
();
List<Employee> newEmployees = employees.hired(TODAY);
assertThat
(newEmployees).hasSize
(6);
Java 8 is picky when choosing the right assertThat
method if the object under test is generic and bounded,
for example if foo is instance of T that extends Exception, java 8 will complain that it can't resolve
the proper assertThat
method (normally assertThat(Throwable)
as foo might implement an interface like List,
if that occurred assertThat(List)
would also be a possible choice - thus confusing java 8.
This why Assertions
have been split in AssertionsForClassTypes
and AssertionsForInterfaceTypes
(see http://stackoverflow.com/questions/29499847/ambiguous-method-in-java-8-why).
Modifier | Constructor and Description |
---|---|
protected |
AssertionsForInterfaceTypes()
Creates a new
. |
Modifier and Type | Method and Description |
---|---|
static <ACTUAL extends Iterable<? extends ELEMENT>,ELEMENT,ELEMENT_ASSERT extends AbstractAssert<ELEMENT_ASSERT,ELEMENT>> |
assertThat(ACTUAL actual,
Class<ELEMENT_ASSERT> assertClass) |
static <T> T |
assertThat(AssertProvider<T> component)
Delegates the creation of the
Assert to the AssertProvider.assertThat() of the given component. |
static AbstractCharSequenceAssert<?,? extends CharSequence> |
assertThat(CharSequence actual)
Creates a new instance of
. |
static <RESULT> CompletableFutureAssert<RESULT> |
assertThat(CompletionStage<RESULT> actual)
Create assertion for
CompletionStage by converting it to a CompletableFuture and returning a CompletableFutureAssert . |
static DoublePredicateAssert |
assertThat(DoublePredicate actual)
Create assertion for
DoublePredicate . |
static ListAssert<Double> |
assertThat(DoubleStream actual)
Creates a new instance of
from the given DoubleStream . |
static IntPredicateAssert |
assertThat(IntPredicate actual)
Create assertion for
IntPredicate . |
static ListAssert<Integer> |
assertThat(IntStream actual)
Creates a new instance of
from the given IntStream . |
static <ELEMENT> IterableAssert<ELEMENT> |
assertThat(Iterable<? extends ELEMENT> actual)
Creates a new instance of
. |
static <ACTUAL extends Iterable<? extends ELEMENT>,ELEMENT,ELEMENT_ASSERT extends AbstractAssert<ELEMENT_ASSERT,ELEMENT>> |
assertThat(Iterable<? extends ELEMENT> actual,
AssertFactory<ELEMENT,ELEMENT_ASSERT> assertFactory)
Creates a new instance of
. |
static <ELEMENT> IteratorAssert<ELEMENT> |
assertThat(Iterator<? extends ELEMENT> actual)
Creates a new instance of
. |
static <ELEMENT> ListAssert<ELEMENT> |
assertThat(List<? extends ELEMENT> actual)
Creates a new instance of
. |
static <ACTUAL extends List<? extends ELEMENT>,ELEMENT,ELEMENT_ASSERT extends AbstractAssert<ELEMENT_ASSERT,ELEMENT>> |
assertThat(List<? extends ELEMENT> actual,
AssertFactory<ELEMENT,ELEMENT_ASSERT> assertFactory) |
static <ELEMENT,ACTUAL extends List<? extends ELEMENT>,ELEMENT_ASSERT extends AbstractAssert<ELEMENT_ASSERT,ELEMENT>> |
assertThat(List<? extends ELEMENT> actual,
Class<ELEMENT_ASSERT> assertClass) |
static LongPredicateAssert |
assertThat(LongPredicate actual)
Create assertion for
LongPredicate . |
static ListAssert<Long> |
assertThat(LongStream actual)
Creates a new instance of
from the given LongStream . |
static <K,V> MapAssert<K,V> |
assertThat(Map<K,V> actual)
Creates a new instance of
. |
static AbstractPathAssert<?> |
assertThat(Path actual)
Creates a new instance of
PathAssert |
static <T> PredicateAssert<T> |
assertThat(Predicate<T> actual)
Create assertion for
Predicate . |
static <ELEMENT> ListAssert<ELEMENT> |
assertThat(Stream<? extends ELEMENT> actual)
Creates a new instance of
from the given Stream . |
static <T extends Comparable<? super T>> |
assertThat(T actual)
Creates a new instance of
with
standard comparison semantics. |
static <T extends AssertDelegateTarget> |
assertThat(T assertion)
Returns the given assertion.
|
allOf, allOf, anyOf, anyOf, assertThatCode, assertThatExceptionOfType, assertThatThrownBy, assertThatThrownBy, atIndex, catchThrowable, catchThrowableOfType, contentOf, contentOf, contentOf, contentOf, contentOf, contentOf, doesNotHave, entry, extractProperty, extractProperty, fail, fail, failBecauseExceptionWasNotThrown, filter, filter, in, linesOf, linesOf, linesOf, linesOf, linesOf, linesOf, not, not, notIn, offset, offset, registerCustomDateFormat, registerCustomDateFormat, setAllowComparingPrivateFields, setAllowExtractingPrivateFields, setLenientDateParsing, setMaxLengthForSingleLineDescription, setRemoveAssertJRelatedElementsFromStackTrace, shouldHaveThrown, tuple, useDefaultDateFormatsOnly, within, within, within, within, within, within, within, withinPercentage, withinPercentage, withinPercentage
protected AssertionsForInterfaceTypes()
Assertions
.public static <T> T assertThat(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 AbstractCharSequenceAssert<?,? extends CharSequence> assertThat(CharSequence actual)
CharSequenceAssert
.actual
- the actual value.public static <ELEMENT> IterableAssert<ELEMENT> assertThat(Iterable<? extends ELEMENT> actual)
IterableAssert
.ELEMENT
- the type of elements.actual
- the actual value.public static <ELEMENT> IteratorAssert<ELEMENT> assertThat(Iterator<? extends ELEMENT> actual)
IteratorAssert
.ELEMENT
- the type of elements.actual
- the actual value.public static <ELEMENT> ListAssert<ELEMENT> assertThat(List<? extends ELEMENT> actual)
ListAssert
.ELEMENT
- the type of elements.actual
- the actual value.public static <ELEMENT> ListAssert<ELEMENT> assertThat(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
assertThat(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.
assertThat(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);
assertThat(stream).isEqualTo(stream)
.isSameAs(stream);
ELEMENT
- the type of elements.actual
- the actual Stream
value.public static ListAssert<Double> assertThat(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
assertThat(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.
assertThat(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);
assertThat(stream).isEqualTo(stream)
.isSameAs(stream);
actual
- the actual DoubleStream
value.public static ListAssert<Long> assertThat(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
assertThat(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.
assertThat(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);
assertThat(stream).isEqualTo(stream)
.isSameAs(stream);
actual
- the actual LongStream
value.public static ListAssert<Integer> assertThat(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
assertThat(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.
assertThat(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);
assertThat(stream).isEqualTo(stream)
.isSameAs(stream);
actual
- the actual IntStream
value.public static <ACTUAL extends Iterable<? extends ELEMENT>,ELEMENT,ELEMENT_ASSERT extends AbstractAssert<ELEMENT_ASSERT,ELEMENT>> FactoryBasedNavigableIterableAssert<?,ACTUAL,ELEMENT,ELEMENT_ASSERT> assertThat(Iterable<? extends ELEMENT> actual, AssertFactory<ELEMENT,ELEMENT_ASSERT> assertFactory)
IterableAssert
.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> assertThat(ACTUAL actual, Class<ELEMENT_ASSERT> assertClass)
public static <ACTUAL extends List<? extends ELEMENT>,ELEMENT,ELEMENT_ASSERT extends AbstractAssert<ELEMENT_ASSERT,ELEMENT>> FactoryBasedNavigableListAssert<?,ACTUAL,ELEMENT,ELEMENT_ASSERT> assertThat(List<? extends ELEMENT> actual, AssertFactory<ELEMENT,ELEMENT_ASSERT> assertFactory)
public static <ELEMENT,ACTUAL extends List<? extends ELEMENT>,ELEMENT_ASSERT extends AbstractAssert<ELEMENT_ASSERT,ELEMENT>> ClassBasedNavigableListAssert<?,ACTUAL,ELEMENT,ELEMENT_ASSERT> assertThat(List<? extends ELEMENT> actual, Class<ELEMENT_ASSERT> assertClass)
public static AbstractPathAssert<?> assertThat(Path actual)
PathAssert
actual
- the path to testpublic static <K,V> MapAssert<K,V> assertThat(Map<K,V> actual)
MapAssert
.
Returned type is MapAssert
as it overrides method to annotate them with SafeVarargs
avoiding
annoying warnings.
K
- the type of keys in the map.V
- the type of values in the map.actual
- the actual value.public static <T extends Comparable<? super T>> AbstractComparableAssert<?,T> assertThat(T actual)
GenericComparableAssert
with
standard comparison semantics.T
- the type of actual.actual
- the actual value.public static <T extends AssertDelegateTarget> T assertThat(T assertion)
assertThat
.
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.assertThat
assertThat(button.isBlinking()).isTrue();
}
void isNotBlinking() {
// standard assertion from core Assertions.assertThat
assertThat(button.isBlinking()).isFalse();
}
}
As MyButtonAssert implements AssertDelegateTarget, you can use assertThat(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 assertThat
assertThat(buttonAssert).isNotBlinking(); // same as : buttonAssert.isNotBlinking();
button.setBlinking(true);
assertThat(buttonAssert).isBlinking(); // same as : buttonAssert.isBlinking();
}
T
- the generic type of the user-defined assert.assertion
- the assertion to return.public static <T> PredicateAssert<T> assertThat(Predicate<T> actual)
Predicate
.T
- the type of the value contained in the Predicate
.actual
- the actual value.public static IntPredicateAssert assertThat(IntPredicate actual)
IntPredicate
.actual
- the actual value.public static LongPredicateAssert assertThat(LongPredicate actual)
LongPredicate
.actual
- the actual value.public static DoublePredicateAssert assertThat(DoublePredicate actual)
DoublePredicate
.actual
- the actual value.public static <RESULT> CompletableFutureAssert<RESULT> assertThat(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.Copyright © 2014–2019 AssertJ. All rights reserved.