Class AssertionsForInterfaceTypes
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).
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic <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) static AbstractCharSequenceAssert<?,
? extends CharSequence> assertThat
(CharSequence actual) Creates a new instance of
.CharSequenceAssert
static <ELEMENT> IterableAssert<ELEMENT>
assertThat
(Iterable<? extends ELEMENT> actual) Creates a new instance of
.IterableAssert
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) Creates a new instance of
.IterableAssert
static AbstractPathAssert<?>
assertThat
(Path actual) Creates a new instance ofPathAssert
static DoublePredicateAssert
assertThat
(DoublePredicate actual) Create assertion forDoublePredicate
.static IntPredicateAssert
assertThat
(IntPredicate actual) Create assertion forIntPredicate
.static LongPredicateAssert
assertThat
(LongPredicate actual) Create assertion forLongPredicate
.static <T> PredicateAssert<T>
assertThat
(Predicate<T> actual) Create assertion forPredicate
.static <ELEMENT> IterableAssert<ELEMENT>
assertThat
(Iterator<? extends ELEMENT> actual) Creates a new instance of
.IterableAssert
static <ELEMENT> ListAssert<ELEMENT>
assertThat
(List<? extends ELEMENT> actual) Creates a new instance of
.ListAssert
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) 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) static <K,
V> MapAssert<K, V> assertThat
(Map<K, V> actual) Creates a new instance of
.MapAssert
static <ELEMENT,
STREAM extends BaseStream<ELEMENT, STREAM>>
ListAssert<ELEMENT>assertThat
(BaseStream<? extends ELEMENT, STREAM> actual) Creates a new instance of
from the givenListAssert
BaseStream
.static <T> T
assertThat
(AssertProvider<T> component) Delegates the creation of theAssert
to theAssertProvider.assertThat()
of the given component.static <T extends Comparable<? super T>>
AbstractComparableAssert<?,T> assertThat
(T actual) Creates a new instance of
with standard comparison semantics.GenericComparableAssert
Methods inherited from class org.assertj.core.api.AssertionsForClassTypes
allOf, allOf, anyOf, anyOf, assertThat, assertThat, assertThat, assertThat, assertThat, assertThat, assertThat, assertThat, assertThat, assertThat, assertThat, assertThat, assertThat, assertThat, assertThat, assertThat, assertThat, assertThat, assertThat, assertThat, assertThat, assertThat, assertThat, assertThat, assertThat, assertThat, assertThat, assertThat, assertThat, assertThat, assertThat, assertThat, assertThat, assertThat, assertThat, assertThat, assertThat, assertThat, assertThat, assertThat, assertThat, assertThat, assertThat, assertThat, assertThat, assertThat, assertThat, assertThatCode, assertThatExceptionOfType, assertThatThrownBy, atIndex, catchThrowable, 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
-
Constructor Details
-
AssertionsForInterfaceTypes
protected AssertionsForInterfaceTypes()Creates a newAssertions
.
-
-
Method Details
-
assertThat
Delegates the creation of theAssert
to theAssertProvider.assertThat()
of the given component.Read the comments on
AssertProvider
for an example of its usage.- Parameters:
component
- the component that creates its own assert- Returns:
- the associated
Assert
of the given component
-
assertThat
Creates a new instance of
.CharSequenceAssert
- Parameters:
actual
- the actual value.- Returns:
- the created assertion object.
-
assertThat
Creates a new instance of
.IterableAssert
- Parameters:
actual
- the actual value.- Returns:
- the created assertion object.
-
assertThat
Creates a new instance of
.IterableAssert
Be aware that calls to most methods on returned IterableAssert will consume Iterator so it won't be possible to iterate over it again. Calling multiple methods on returned IterableAssert is safe as Iterator's elements are cached by IterableAssert first time Iterator is consumed.
- Parameters:
actual
- the actual value.- Returns:
- the created assertion object.
-
assertThat
Creates a new instance of
.ListAssert
- Parameters:
actual
- the actual value.- Returns:
- the created assertion object.
-
assertThat
public static <ELEMENT,STREAM extends BaseStream<ELEMENT, ListAssert<ELEMENT> assertThatSTREAM>> (BaseStream<? extends ELEMENT, STREAM> actual) Creates a new instance of
from the givenListAssert
BaseStream
.Be aware that to create the returned
ListAssert
the given theBaseStream
is consumed so it won't be possible to use it again. Calling multiple methods on the returnedListAssert
is safe as it only interacts with theList
built from theBaseStream
.This method accepts
Stream
and primitive stream variantsIntStream
,LongStream
andDoubleStream
.- Parameters:
actual
- the actualBaseStream
value.- Returns:
- the created assertion object.
-
assertThat
public static <ACTUAL extends Iterable<? extends ELEMENT>,ELEMENT, FactoryBasedNavigableIterableAssert<?,ELEMENT_ASSERT extends AbstractAssert<ELEMENT_ASSERT, ELEMENT>> ACTUAL, assertThatELEMENT, ELEMENT_ASSERT> (Iterable<? extends ELEMENT> actual, AssertFactory<ELEMENT, ELEMENT_ASSERT> assertFactory) Creates a new instance of
.IterableAssert
- Parameters:
actual
- the actual value.- Returns:
- the created assertion object.
-
assertThat
public static <ACTUAL extends Iterable<? extends ELEMENT>,ELEMENT, ClassBasedNavigableIterableAssert<?,ELEMENT_ASSERT extends AbstractAssert<ELEMENT_ASSERT, ELEMENT>> ACTUAL, assertThatELEMENT, ELEMENT_ASSERT> (ACTUAL actual, Class<ELEMENT_ASSERT> assertClass) -
assertThat
public static <ACTUAL extends List<? extends ELEMENT>,ELEMENT, FactoryBasedNavigableListAssert<?,ELEMENT_ASSERT extends AbstractAssert<ELEMENT_ASSERT, ELEMENT>> ACTUAL, assertThatELEMENT, ELEMENT_ASSERT> (List<? extends ELEMENT> actual, AssertFactory<ELEMENT, ELEMENT_ASSERT> assertFactory) -
assertThat
public static <ELEMENT,ACTUAL extends List<? extends ELEMENT>, ClassBasedNavigableListAssert<?,ELEMENT_ASSERT extends AbstractAssert<ELEMENT_ASSERT, ELEMENT>> ACTUAL, assertThatELEMENT, ELEMENT_ASSERT> (List<? extends ELEMENT> actual, Class<ELEMENT_ASSERT> assertClass) -
assertThat
Creates a new instance ofPathAssert
- Parameters:
actual
- the path to test- Returns:
- the created assertion object
-
assertThat
Creates a new instance of
.MapAssert
Returned type is
MapAssert
as it overrides method to annotate them withSafeVarargs
avoiding annoying warnings.- Parameters:
actual
- the actual value.- Returns:
- the created assertion object.
-
assertThat
Creates a new instance of
with standard comparison semantics.GenericComparableAssert
- Parameters:
actual
- the actual value.- Returns:
- the created assertion object.
-
assertThat
Returns the given assertion. This method improves code readability by surrounding the given assertion withassertThat
.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.assertThat assertThat(button.isBlinking()).isTrue(); } void isNotBlinking() { // standard assertion from core Assertions.assertThat assertThat(button.isBlinking()).isFalse(); } }
assertThat(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 assertThat assertThat(buttonAssert).isNotBlinking(); // same as : buttonAssert.isNotBlinking(); button.setBlinking(true); assertThat(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.
-
assertThat
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
-
assertThat
Create assertion forIntPredicate
.- Parameters:
actual
- the actual value.- Returns:
- the created assertion object.
- Since:
- 3.5.0
-
assertThat
Create assertion forLongPredicate
.- Parameters:
actual
- the actual value.- Returns:
- the created assertion object.
- Since:
- 3.5.0
-
assertThat
Create assertion forDoublePredicate
.- Parameters:
actual
- the actual value.- Returns:
- the created assertion object.
- Since:
- 3.5.0
-