public class Assertions extends StrictAssertions
For example:
int removed = employees.removeFired();
assertThat(removed).isZero();
List<Employee> newEmployees = employees.hired(TODAY);
assertThat(newEmployees).hasSize(6);
The difference with StrictAssertions is that this class contains assertThat methods with interface parameter
to avoid Java 8 ambiguous method error (see
http://stackoverflow.com/questions/29499847/ambiguous-method-in-java-8-why).| Modifier | Constructor and Description |
|---|---|
protected |
Assertions()
Creates a new
Assertions. |
| Modifier and Type | Method and Description |
|---|---|
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 <T> AbstractIterableAssert<?,? extends Iterable<? extends T>,T> |
assertThat(Iterable<? extends T> actual)
Creates a new instance of
. |
static <T> AbstractIterableAssert<?,? extends Iterable<? extends T>,T> |
assertThat(Iterator<? extends T> actual)
Creates a new instance of
. |
static <T> AbstractListAssert<?,? extends List<? extends T>,T> |
assertThat(List<? extends T> actual)
Creates a new instance of
. |
static <K,V> MapAssert<K,V> |
assertThat(Map<K,V> actual)
Creates a new instance of
. |
static AbstractPathAssert<?> |
assertThat(java.nio.file.Path actual)
Creates a new instance of
PathAssert |
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, 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, withinPercentageprotected Assertions()
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.
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 <T> AbstractIterableAssert<?,? extends Iterable<? extends T>,T> assertThat(Iterable<? extends T> actual)
IterableAssert.actual - the actual value.public static <T> AbstractIterableAssert<?,? extends Iterable<? extends T>,T> assertThat(Iterator<? extends T> actual)
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.actual - the actual value.public static <T> AbstractListAssert<?,? extends List<? extends T>,T> assertThat(List<? extends T> actual)
ListAssert.actual - the actual value.public static AbstractPathAssert<?> assertThat(java.nio.file.Path actual)
PathAssertactual - 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.
actual - the actual value.public static <T extends Comparable<? super T>> AbstractComparableAssert<?,T> assertThat(T actual)
GenericComparableAssert with
standard comparison semantics.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.Copyright © 2014-2015 AssertJ. All Rights Reserved.