public class Assertions extends Object
For example:
int removed = employees.removeFired();
assertThat
(removed).isZero
();
List<Employee> newEmployees = employees.hired(TODAY);
assertThat
(newEmployees).hasSize
(6);
Modifier | Constructor and Description |
---|---|
protected |
Assertions()
Creates a new
Assertions . |
Modifier and Type | Method and Description |
---|---|
static <T> Condition<T> |
allOf(Condition<? super T>... conditions)
Creates a new
|
static <T> Condition<T> |
allOf(Iterable<? extends Condition<? super T>> conditions)
Creates a new
|
static <T> Condition<T> |
anyOf(Condition<? super T>... conditions)
Only delegate to
AnyOf.anyOf(Condition...) so that Assertions offers a full feature entry point to all
AssertJ features (but you can use AnyOf if you prefer). |
static <T> Condition<T> |
anyOf(Iterable<? extends Condition<? super T>> conditions)
Creates a new
|
static <T> T |
assertThat(AssertProvider<T> component)
Delegates the creation of the
Assert to the AssertProvider.assertThat() of the given component. |
static AbstractBigDecimalAssert<?> |
assertThat(BigDecimal actual)
Creates a new instance of
. |
static AbstractBooleanAssert<?> |
assertThat(boolean actual)
Creates a new instance of
. |
static AbstractBooleanAssert<?> |
assertThat(Boolean actual)
Creates a new instance of
. |
static AbstractBooleanArrayAssert<?> |
assertThat(boolean[] actual)
Creates a new instance of
. |
static AbstractByteAssert<?> |
assertThat(byte actual)
Creates a new instance of
. |
static AbstractByteAssert<?> |
assertThat(Byte actual)
Creates a new instance of
. |
static AbstractByteArrayAssert<?> |
assertThat(byte[] actual)
Creates a new instance of
. |
static AbstractCharacterAssert<?> |
assertThat(char actual)
Creates a new instance of
. |
static AbstractCharArrayAssert<?> |
assertThat(char[] actual)
Creates a new instance of
. |
static AbstractCharacterAssert<?> |
assertThat(Character actual)
Creates a new instance of
. |
static AbstractCharSequenceAssert<?,? extends CharSequence> |
assertThat(CharSequence actual)
Creates a new instance of
. |
static AbstractClassAssert<?> |
assertThat(Class<?> actual)
Creates a new instance of
|
static AbstractDateAssert<?> |
assertThat(Date actual)
Creates a new instance of
. |
static AbstractDoubleAssert<?> |
assertThat(double actual)
Creates a new instance of
. |
static AbstractDoubleAssert<?> |
assertThat(Double actual)
Creates a new instance of
. |
static AbstractDoubleArrayAssert<?> |
assertThat(double[] actual)
Creates a new instance of
. |
static AbstractFileAssert<?> |
assertThat(File actual)
Creates a new instance of
. |
static AbstractFloatAssert<?> |
assertThat(float actual)
Creates a new instance of
. |
static AbstractFloatAssert<?> |
assertThat(Float actual)
Creates a new instance of
. |
static AbstractFloatArrayAssert<?> |
assertThat(float[] actual)
Creates a new instance of
. |
static AbstractInputStreamAssert<?,? extends InputStream> |
assertThat(InputStream actual)
Creates a new instance of
. |
static AbstractIntegerAssert<?> |
assertThat(int actual)
Creates a new instance of
. |
static AbstractIntArrayAssert<?> |
assertThat(int[] actual)
Creates a new instance of
. |
static AbstractIntegerAssert<?> |
assertThat(Integer 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 AbstractLocalDateAssert<?> |
assertThat(java.time.LocalDate localDate)
Creates a new instance of
. |
static AbstractLocalDateTimeAssert<?> |
assertThat(java.time.LocalDateTime localDateTime)
Creates a new instance of
. |
static AbstractLocalTimeAssert<?> |
assertThat(java.time.LocalTime actual)
Creates a new instance of
. |
static AbstractLongAssert<?> |
assertThat(long actual)
Creates a new instance of
. |
static AbstractLongAssert<?> |
assertThat(Long actual)
Creates a new instance of
. |
static AbstractLongArrayAssert<?> |
assertThat(long[] actual)
Creates a new instance of
. |
static <K,V> MapAssert<K,V> |
assertThat(Map<K,V> actual)
Creates a new instance of
. |
static <T> OptionalAssert<T> |
assertThat(Optional<T> optional)
Create assertion for
Optional . |
static AbstractPathAssert<?> |
assertThat(java.nio.file.Path actual)
Creates a new instance of
PathAssert |
static AbstractShortAssert<?> |
assertThat(short actual)
Creates a new instance of
. |
static AbstractShortAssert<?> |
assertThat(Short actual)
Creates a new instance of
. |
static AbstractShortArrayAssert<?> |
assertThat(short[] actual)
Creates a new instance of
. |
static AbstractCharSequenceAssert<?,String> |
assertThat(String actual)
Creates a new instance of
. |
static <T> AbstractObjectAssert<?,T> |
assertThat(T actual)
Creates a new instance of
. |
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.
|
static <T> AbstractObjectArrayAssert<?,T> |
assertThat(T[] actual)
Creates a new instance of
. |
static AbstractThrowableAssert<?,? extends Throwable> |
assertThat(Throwable actual)
Creates a new instance of
. |
static AbstractZonedDateTimeAssert<?> |
assertThat(java.time.ZonedDateTime date)
Creates a new instance of
. |
static AbstractThrowableAssert<?,? extends Throwable> |
assertThatThrownBy(ThrowableAssert.ThrowingCallable shouldRaiseThrowable)
Allows to capture and then assert on a
Throwable more easily when used with Java 8 lambdas. |
static Index |
atIndex(int index)
Only delegate to
Index.atIndex(int) so that Assertions offers a full feature entry point to all AssertJ
features (but you can use Index if you prefer). |
static Throwable |
catchThrowable(ThrowableAssert.ThrowingCallable shouldRaiseThrowable)
Allows to catch an
Throwable more easily when used with Java 8 lambdas. |
static String |
contentOf(File file)
Loads the text content of a file with the default character set, so that it can be passed to
assertThat(String) . |
static String |
contentOf(File file,
Charset charset)
Loads the text content of a file, so that it can be passed to
assertThat(String) . |
static String |
contentOf(File file,
String charsetName)
Loads the text content of a file, so that it can be passed to
assertThat(String) . |
static <T> DoesNotHave<T> |
doesNotHave(Condition<? super T> condition)
Creates a new
DoesNotHave . |
static <K,V> MapEntry<K,V> |
entry(K key,
V value)
Only delegate to
MapEntry#entry(K key, V value) so that Assertions offers a full feature entry point to
all
AssertJ features (but you can use MapEntry if you prefer). |
static Properties<Object> |
extractProperty(String propertyName)
Only delegate to
Properties.extractProperty(String) so that Assertions offers a full feature entry point
to
all AssertJ features (but you can use Properties if you prefer). |
static <T> Properties<T> |
extractProperty(String propertyName,
Class<T> propertyType)
Only delegate to
Properties.extractProperty(String) so that Assertions offers a full feature entry point
to
all AssertJ features (but you can use Properties if you prefer). |
static void |
fail(String failureMessage)
Only delegate to
Fail.fail(String) so that Assertions offers a full feature entry point to all Assertj
Assert features (but you can use Fail if you prefer). |
static void |
fail(String failureMessage,
Throwable realCause)
Only delegate to
Fail.fail(String, Throwable) so that Assertions offers a full feature entry point to all
AssertJ features (but you can use Fail if you prefer). |
static void |
failBecauseExceptionWasNotThrown(Class<? extends Throwable> exceptionClass)
Only delegate to
Fail.failBecauseExceptionWasNotThrown(Class) so that Assertions offers a full feature
entry point to all AssertJ features (but you can use Fail if you prefer). |
static <E> Filters<E> |
filter(E[] array)
Only delegate to
Filters.filter(Object[]) so that Assertions offers a full feature entry point to all
AssertJ features (but you can use Filters if you prefer). |
static <E> Filters<E> |
filter(Iterable<E> iterableToFilter)
Only delegate to
Filters.filter(Object[]) so that Assertions offers a full feature entry point to all
AssertJ features (but you can use Filters if you prefer). |
static List<String> |
linesOf(File file)
Loads the text content of a file into a list of strings with the default charset, each string corresponding to a
line.
|
static List<String> |
linesOf(File file,
Charset charset)
Loads the text content of a file into a list of strings, each string corresponding to a line.
|
static List<String> |
linesOf(File file,
String charsetName)
Loads the text content of a file into a list of strings, each string corresponding to a line.
|
static <T> Not<T> |
not(Condition<? super T> condition)
Creates a new
Not . |
static Offset<Double> |
offset(Double value)
Assertions entry point for double
Offset . |
static Offset<Float> |
offset(Float value)
Assertions entry point for float
Offset . |
static void |
registerCustomDateFormat(DateFormat userCustomDateFormat)
Add the given date format to the ones used to parse date String in String based Date assertions like
AbstractDateAssert.isEqualTo(String) . |
static void |
registerCustomDateFormat(String userCustomDateFormatPattern)
Add the given date format to the ones used to parse date String in String based Date assertions like
AbstractDateAssert.isEqualTo(String) . |
static void |
setAllowComparingPrivateFields(boolean allowComparingPrivateFields)
Globally sets whether the use of private fields is allowed for comparison.
|
static void |
setAllowExtractingPrivateFields(boolean allowExtractingPrivateFields)
Globally sets whether
and
should be allowed to extract private fields, if not and they try it fails with exception. |
static void |
setRemoveAssertJRelatedElementsFromStackTrace(boolean removeAssertJRelatedElementsFromStackTrace)
Only delegate to
Fail.setRemoveAssertJRelatedElementsFromStackTrace(boolean) so that Assertions offers a
full feature entry point to all AssertJ Assert features (but you can use Fail if you prefer). |
static void |
shouldHaveThrown(Class<? extends Throwable> exceptionClass)
Only delegate to
Fail.shouldHaveThrown(Class) so that Assertions offers a full feature
entry point to all AssertJ features (but you can use Fail if you prefer). |
static Tuple |
tuple(Object... values)
Utility method to build nicely a
Tuple when working with AbstractIterableAssert.extracting(String...) or
AbstractObjectArrayAssert.extracting(String...) |
static void |
useDefaultDateFormatsOnly()
Remove all registered custom date formats => use only the defaults date formats to parse string as date.
|
static Offset<BigDecimal> |
within(BigDecimal value)
Assertions entry point for BigDecimal
Offset to use with isCloseTo assertions. |
static Offset<Byte> |
within(Byte value)
Assertions entry point for Byte
Offset to use with isCloseTo assertions. |
static Offset<Double> |
within(Double value)
Alias for
offset(Double) to use with isCloseTo assertions. |
static Offset<Float> |
within(Float value)
Alias for
offset(Float) to use with isCloseTo assertions. |
static Offset<Integer> |
within(Integer value)
Assertions entry point for Integer
Offset to use with isCloseTo assertions. |
static Offset<Long> |
within(Long value)
Assertions entry point for Long
Offset to use with isCloseTo assertions. |
static Offset<Short> |
within(Short value)
Assertions entry point for Short
Offset to use with isCloseTo assertions. |
protected Assertions()
Assertions
.public static <T> OptionalAssert<T> assertThat(Optional<T> optional)
Optional
.T
- the type of the value contained in the Optional
.optional
- the actual value.public static AbstractBigDecimalAssert<?> assertThat(BigDecimal actual)
BigDecimalAssert
.actual
- the actual value.public static AbstractBooleanAssert<?> assertThat(boolean actual)
BooleanAssert
.actual
- the actual value.public static AbstractBooleanAssert<?> assertThat(Boolean actual)
BooleanAssert
.actual
- the actual value.public static AbstractBooleanArrayAssert<?> assertThat(boolean[] actual)
BooleanArrayAssert
.actual
- the actual value.public static AbstractByteAssert<?> assertThat(byte actual)
ByteAssert
.actual
- the actual value.public static AbstractByteAssert<?> assertThat(Byte actual)
ByteAssert
.actual
- the actual value.public static AbstractByteArrayAssert<?> assertThat(byte[] actual)
ByteArrayAssert
.actual
- the actual value.public static AbstractCharacterAssert<?> assertThat(char actual)
CharacterAssert
.actual
- the actual value.public static AbstractCharArrayAssert<?> assertThat(char[] actual)
CharArrayAssert
.actual
- the actual value.public static AbstractCharacterAssert<?> assertThat(Character actual)
CharacterAssert
.actual
- the actual value.public static AbstractClassAssert<?> assertThat(Class<?> actual)
ClassAssert
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> 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 AbstractDoubleAssert<?> assertThat(double actual)
DoubleAssert
.actual
- the actual value.public static AbstractDoubleAssert<?> assertThat(Double actual)
DoubleAssert
.actual
- the actual value.public static AbstractDoubleArrayAssert<?> assertThat(double[] actual)
DoubleArrayAssert
.actual
- the actual value.public static AbstractFileAssert<?> assertThat(File actual)
FileAssert
.actual
- the actual value.public static AbstractPathAssert<?> assertThat(java.nio.file.Path actual)
PathAssert
actual
- the path to testpublic static AbstractInputStreamAssert<?,? extends InputStream> assertThat(InputStream actual)
InputStreamAssert
.actual
- the actual value.public static AbstractFloatAssert<?> assertThat(float actual)
FloatAssert
.actual
- the actual value.public static AbstractFloatAssert<?> assertThat(Float actual)
FloatAssert
.actual
- the actual value.public static AbstractFloatArrayAssert<?> assertThat(float[] actual)
FloatArrayAssert
.actual
- the actual value.public static AbstractIntegerAssert<?> assertThat(int actual)
IntegerAssert
.actual
- the actual value.public static AbstractIntArrayAssert<?> assertThat(int[] actual)
IntArrayAssert
.actual
- the actual value.public static AbstractIntegerAssert<?> assertThat(Integer actual)
IntegerAssert
.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 AbstractLongAssert<?> assertThat(long actual)
LongAssert
.actual
- the actual value.public static AbstractLongAssert<?> assertThat(Long actual)
LongAssert
.actual
- the actual value.public static AbstractLongArrayAssert<?> assertThat(long[] actual)
LongArrayAssert
.actual
- the actual value.public static <T> AbstractObjectAssert<?,T> assertThat(T actual)
ObjectAssert
.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> 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 <T> AbstractObjectArrayAssert<?,T> assertThat(T[] actual)
ObjectArrayAssert
.actual
- the actual value.public 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 AbstractShortAssert<?> assertThat(short actual)
ShortAssert
.actual
- the actual value.public static AbstractShortAssert<?> assertThat(Short actual)
ShortAssert
.actual
- the actual value.public static AbstractShortArrayAssert<?> assertThat(short[] actual)
ShortArrayAssert
.actual
- the actual value.public static AbstractCharSequenceAssert<?,? extends CharSequence> assertThat(CharSequence actual)
CharSequenceAssert
.actual
- the actual value.public static AbstractCharSequenceAssert<?,String> assertThat(String actual)
StringAssert
.actual
- the actual value.public static AbstractDateAssert<?> assertThat(Date actual)
DateAssert
.actual
- the actual value.public static AbstractZonedDateTimeAssert<?> assertThat(java.time.ZonedDateTime date)
ZonedDateTimeAssert
.actual
- the actual value.public static AbstractLocalDateTimeAssert<?> assertThat(java.time.LocalDateTime localDateTime)
LocalDateTimeAssert
.actual
- the actual value.public static AbstractLocalTimeAssert<?> assertThat(java.time.LocalTime actual)
LocalTimeAssert
.actual
- the actual value.public static AbstractLocalDateAssert<?> assertThat(java.time.LocalDate localDate)
LocalDateAssert
.actual
- the actual value.public static AbstractThrowableAssert<?,? extends Throwable> assertThat(Throwable actual)
ThrowableAssert
.actual
- the actual value.ThrowableAssert
.public static AbstractThrowableAssert<?,? extends Throwable> assertThatThrownBy(ThrowableAssert.ThrowingCallable shouldRaiseThrowable)
Throwable
more easily when used with Java 8 lambdas.
Example :
@Test
public void testException() {
assertThatThrownBy(() -> { throw new Exception("boom!") }).isInstanceOf(Exception.class)
.hasMessageContaining("boom");
}
shouldRaiseThrowable
- The ThrowableAssert.ThrowingCallable
or lambda with the code that should raise the throwable.null
if none was raised by the callable.public static Throwable catchThrowable(ThrowableAssert.ThrowingCallable shouldRaiseThrowable)
Throwable
more easily when used with Java 8 lambdas.
This caught Throwable
can then be asserted.
Example:
@Test
public void testException() {
// when
Throwable thrown = catchThrowable(() -> { throw new Exception("boom!") });
// then
assertThat(thrown).isInstanceOf(Exception.class)
.hasMessageContaining("boom");
}
shouldRaiseThrowable
- The lambda with the code that should raise the exception.null
if none was raised by the callable.public static void setRemoveAssertJRelatedElementsFromStackTrace(boolean removeAssertJRelatedElementsFromStackTrace)
Fail.setRemoveAssertJRelatedElementsFromStackTrace(boolean)
so that Assertions offers a
full feature entry point to all AssertJ Assert features (but you can use Fail
if you prefer).public static void fail(String failureMessage)
Fail.fail(String)
so that Assertions offers a full feature entry point to all Assertj
Assert features (but you can use Fail if you prefer).public static void fail(String failureMessage, Throwable realCause)
Fail.fail(String, Throwable)
so that Assertions offers a full feature entry point to all
AssertJ features (but you can use Fail if you prefer).public static void failBecauseExceptionWasNotThrown(Class<? extends Throwable> exceptionClass)
Fail.failBecauseExceptionWasNotThrown(Class)
so that Assertions offers a full feature
entry point to all AssertJ features (but you can use Fail if you prefer).
shouldHaveThrown(Class)
can be used as a replacement.public static void shouldHaveThrown(Class<? extends Throwable> exceptionClass)
Fail.shouldHaveThrown(Class)
so that Assertions offers a full feature
entry point to all AssertJ features (but you can use Fail if you prefer).public static <T> Properties<T> extractProperty(String propertyName, Class<T> propertyType)
Properties.extractProperty(String)
so that Assertions offers a full feature entry point
to
all AssertJ features (but you can use Properties
if you prefer).
Typical usage is to chain extractProperty
with from
method, see examples below :
// extract simple property values having a java standard type (here String)
assertThat(extractProperty("name", String.class).from(fellowshipOfTheRing)).contains("
Boromir", "Gandalf", "Frodo",
"Legolas").doesNotContain("Sauron", "Elrond");
// extracting property works also with user's types (here Race)
assertThat(extractProperty("race", String.class).from(fellowshipOfTheRing)).contains(HOBBIT,
ELF).doesNotContain(ORC);
// extract nested property on Race
assertThat(extractProperty("race.name", String.class).from(fellowshipOfTheRing)).contains("
Hobbit", "Elf")
.doesNotContain("Orc");
public static Properties<Object> extractProperty(String propertyName)
Properties.extractProperty(String)
so that Assertions offers a full feature entry point
to
all AssertJ features (but you can use Properties
if you prefer).
Typical usage is to chain extractProperty
with from
method, see examples below :
// extract simple property values, as no type has been defined the extracted property will be considered as Object
// to define the real property type (here String) use extractProperty("name", String.class) instead.
assertThat(extractProperty("name").from(fellowshipOfTheRing)).contains("Boromir",
"Gandalf", "Frodo", "Legolas")
.doesNotContain("Sauron", "Elrond");
// extracting property works also with user's types (here Race), even though it will be considered as Object
// to define the real property type (here String) use extractProperty("name", Race.class) instead.
assertThat(extractProperty("race").from(fellowshipOfTheRing)).contains(HOBBIT, ELF).doesNotContain(ORC);
// extract nested property on Race
assertThat(extractProperty("race.name").from(fellowshipOfTheRing)).contains("Hobbit",
"Elf").doesNotContain("Orc");
public static Tuple tuple(Object... values)
Tuple
when working with AbstractIterableAssert.extracting(String...)
or
AbstractObjectArrayAssert.extracting(String...)
public static void setAllowExtractingPrivateFields(boolean allowExtractingPrivateFields)
IterableAssert#extracting(String)
and
ObjectArrayAssert#extracting(String)
should be allowed to extract private fields, if not and they try it fails with exception.allowExtractingPrivateFields
- allow private fields extraction. Default true
.public static void setAllowComparingPrivateFields(boolean allowComparingPrivateFields)
AbstractIterableAssert.usingElementComparatorOnFields(java.lang.String...)
org.assertj.core.api.AbstractObjectAssert#isEqualToComparingFieldByField(A)
false
and these methods try to compare private fields, it will fail with an exception.allowComparingPrivateFields
- allow private fields comparison. Default true
.public static <K,V> MapEntry<K,V> entry(K key, V value)
MapEntry#entry(K key, V value)
so that Assertions offers a full feature entry point to
all
AssertJ features (but you can use MapEntry
if you prefer).
Typical usage is to call entry
in MapAssert contains
assertion, see examples below :
Map ringBearers = ... // init omitted
assertThat(ringBearers).contains(entry(oneRing, frodo), entry(nenya, galadriel));
public static Index atIndex(int index)
Index.atIndex(int)
so that Assertions offers a full feature entry point to all AssertJ
features (but you can use Index
if you prefer).
Typical usage :
List<Ring> elvesRings = newArrayList(vilya, nenya, narya);
assertThat(elvesRings).contains(vilya, atIndex(0)).contains(nenya, atIndex(1)).contains(narya, atIndex(2));
public static Offset<Double> offset(Double value)
Offset
.
Typical usage :
assertThat(8.1).isEqualTo(8.0, offset(0.1));
public static Offset<Float> offset(Float value)
Offset
.
Typical usage :
assertThat(8.2f).isCloseTo(8.0f, offset(0.2f));
public static Offset<Double> within(Double value)
offset(Double)
to use with isCloseTo assertions.
Typical usage :
assertThat(8.1).isCloseTo(8.0, within(0.1));
public static Offset<Float> within(Float value)
offset(Float)
to use with isCloseTo assertions.
Typical usage :
assertThat(8.2f).isCloseTo(8.0f, within(0.2f));
public static Offset<BigDecimal> within(BigDecimal value)
Offset
to use with isCloseTo assertions.
Typical usage :
assertThat(BigDecimal.TEN).isCloseTo(new BigDecimal("10.5"), within(BigDecimal.ONE));
public static Offset<Byte> within(Byte value)
Offset
to use with isCloseTo assertions.
Typical usage :
assertThat((byte)10).isCloseTo((byte)11, within((byte)1));
public static Offset<Integer> within(Integer value)
Offset
to use with isCloseTo assertions.
Typical usage :
assertThat(10).isCloseTo(11, within(1));
public static Offset<Short> within(Short value)
Offset
to use with isCloseTo assertions.
Typical usage :
assertThat(10).isCloseTo(11, within(1));
public static Offset<Long> within(Long value)
Offset
to use with isCloseTo assertions.
Typical usage :
assertThat(5l).isCloseTo(7l, within(2l));
@SafeVarargs public static <T> Condition<T> allOf(Condition<? super T>... conditions)
AllOf
T
- the type of object the given condition accept.conditions
- the conditions to evaluate.AnyOf
.NullPointerException
- if the given array is null
.NullPointerException
- if any of the elements in the given array is null
.public static <T> Condition<T> allOf(Iterable<? extends Condition<? super T>> conditions)
AllOf
T
- the type of object the given condition accept.conditions
- the conditions to evaluate.AnyOf
.NullPointerException
- if the given iterable is null
.NullPointerException
- if any of the elements in the given iterable is null
.@SafeVarargs public static <T> Condition<T> anyOf(Condition<? super T>... conditions)
AnyOf.anyOf(Condition...)
so that Assertions offers a full feature entry point to all
AssertJ features (but you can use AnyOf
if you prefer).
Typical usage (jedi
and sith
are Condition
) :
assertThat("Vader").is(anyOf(jedi, sith));
public static <T> Condition<T> anyOf(Iterable<? extends Condition<? super T>> conditions)
AnyOf
T
- the type of object the given condition accept.conditions
- the conditions to evaluate.AnyOf
.NullPointerException
- if the given iterable is null
.NullPointerException
- if any of the elements in the given iterable is null
.public static <T> DoesNotHave<T> doesNotHave(Condition<? super T> condition)
DoesNotHave
.condition
- the condition to inverse.public static <T> Not<T> not(Condition<? super T> condition)
Not
.condition
- the condition to inverse.public static <E> Filters<E> filter(E[] array)
Filters.filter(Object[])
so that Assertions offers a full feature entry point to all
AssertJ features (but you can use Filters
if you prefer).
Note that the given array is not modified, the filters are performed on an Iterable
copy of the array.
Typical usage with Condition
:
assertThat(filter(players).being(potentialMVP).get()).containsOnly(james, rose);
and with filter language based on java bean property :
assertThat(filter(players).with("pointsPerGame").greaterThan(20).and("assistsPerGame")
.greaterThan(7).get())
.containsOnly(james, rose);
public static <E> Filters<E> filter(Iterable<E> iterableToFilter)
Filters.filter(Object[])
so that Assertions offers a full feature entry point to all
AssertJ features (but you can use Filters
if you prefer).
Note that the given Iterable
is not modified, the filters are performed on a copy.
Typical usage with Condition
:
assertThat(filter(players).being(potentialMVP).get()).containsOnly(james, rose);
and with filter language based on java bean property :
assertThat(filter(players).with("pointsPerGame").greaterThan(20).and("assistsPerGame")
.greaterThan(7).get())
.containsOnly(james, rose);
public static String contentOf(File file, Charset charset)
assertThat(String)
.
Note that this will load the entire file in memory; for larger files, there might be a more efficient alternative
with assertThat(File)
.
file
- the file.charset
- the character set to use.NullPointerException
- if the given charset is null
.FilesException
- if an I/O exception occurs.public static String contentOf(File file, String charsetName)
assertThat(String)
.
Note that this will load the entire file in memory; for larger files, there might be a more efficient alternative
with assertThat(File)
.
file
- the file.charsetName
- the name of the character set to use.IllegalArgumentException
- if the given character set is not supported on this platform.FilesException
- if an I/O exception occurs.public static String contentOf(File file)
assertThat(String)
.
Note that this will load the entire file in memory; for larger files, there might be a more efficient alternative
with assertThat(File)
.
file
- the file.FilesException
- if an I/O exception occurs.public static List<String> linesOf(File file)
file
- the file.NullPointerException
- if the given charset is null
.FilesException
- if an I/O exception occurs.public static List<String> linesOf(File file, Charset charset)
file
- the file.charset
- the character set to use.NullPointerException
- if the given charset is null
.FilesException
- if an I/O exception occurs.public static List<String> linesOf(File file, String charsetName)
file
- the file.charsetName
- the name of the character set to use.NullPointerException
- if the given charset is null
.FilesException
- if an I/O exception occurs.public static void registerCustomDateFormat(DateFormat userCustomDateFormat)
AbstractDateAssert.isEqualTo(String)
.
User date formats are used before default ones in the order they have been registered (first registered, first
used).
AssertJ is gonna use any date formats registered with one of these methods :
AbstractDateAssert.withDateFormat(String)
AbstractDateAssert.withDateFormat(java.text.DateFormat)
registerCustomDateFormat(java.text.DateFormat)
registerCustomDateFormat(String)
useDefaultDateFormatsOnly()
or
AbstractDateAssert.withDefaultDateFormatsOnly()
.
Code examples:
Date date = ... // set to 2003 April the 26th
assertThat(date).isEqualTo("2003-04-26");
try {
// date with a custom format : failure since the default formats don't match.
assertThat(date).isEqualTo("2003/04/26");
} catch (AssertionError e) {
assertThat(e).hasMessage("Failed to parse 2003/04/26 with any of these date formats: " +
"[yyyy-MM-dd'T'HH:mm:ss.SSS, yyyy-MM-dd'T'HH:mm:ss, yyyy-MM-dd]");
}
// registering a custom date format to make the assertion pass
registerCustomDateFormat(new SimpleDateFormat("yyyy/MM/dd")); // registerCustomDateFormat("yyyy/MM/dd") would work to.
assertThat(date).isEqualTo("2003/04/26");
// the default formats are still available and should work
assertThat(date).isEqualTo("2003-04-26");
userCustomDateFormat
- the new Date format used for String based Date assertions.public static void registerCustomDateFormat(String userCustomDateFormatPattern)
AbstractDateAssert.isEqualTo(String)
.
User date formats are used before default ones in the order they have been registered (first registered, first
used).
AssertJ is gonna use any date formats registered with one of these methods :
AbstractDateAssert.withDateFormat(String)
AbstractDateAssert.withDateFormat(java.text.DateFormat)
registerCustomDateFormat(java.text.DateFormat)
registerCustomDateFormat(String)
useDefaultDateFormatsOnly()
or
AbstractDateAssert.withDefaultDateFormatsOnly()
.
Code examples:
Date date = ... // set to 2003 April the 26th
assertThat(date).isEqualTo("2003-04-26");
try {
// date with a custom format : failure since the default formats don't match.
assertThat(date).isEqualTo("2003/04/26");
} catch (AssertionError e) {
assertThat(e).hasMessage("Failed to parse 2003/04/26 with any of these date formats: " +
"[yyyy-MM-dd'T'HH:mm:ss.SSS, yyyy-MM-dd'T'HH:mm:ss, yyyy-MM-dd]");
}
// registering a custom date format to make the assertion pass
registerCustomDateFormat("yyyy/MM/dd");
assertThat(date).isEqualTo("2003/04/26");
// the default formats are still available and should work
assertThat(date).isEqualTo("2003-04-26");
userCustomDateFormatPattern
- the new Date format pattern used for String based Date assertions.public static void useDefaultDateFormatsOnly()
yyyy-MM-dd'T'HH:mm:ss.SSS
yyyy-MM-dd'T'HH:mm:ss
yyyy-MM-dd
2003-04-26T03:01:02.999
2003-04-26T13:01:02
2003-04-26
Copyright © 2014-2015 AssertJ. All Rights Reserved.