public class Java6Assertions extends Object
Assertions
.Assertions
Modifier | Constructor and Description |
---|---|
protected |
Java6Assertions() |
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 <ACTUAL extends Iterable<? extends ELEMENT>,ELEMENT,ELEMENT_ASSERT extends AbstractAssert<ELEMENT_ASSERT,ELEMENT>> |
assertThat(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 |
assertThat(AssertProvider<T> component)
Delegates the creation of the
Assert to the AssertProvider.assertThat() of the given component. |
static AtomicBooleanAssert |
assertThat(AtomicBoolean actual)
Create assertion for
AtomicBoolean . |
static AtomicIntegerAssert |
assertThat(AtomicInteger actual)
Create assertion for
AtomicInteger . |
static AtomicIntegerArrayAssert |
assertThat(AtomicIntegerArray actual)
Create int[] assertion for
AtomicIntegerArray . |
static <OBJECT> AtomicIntegerFieldUpdaterAssert<OBJECT> |
assertThat(AtomicIntegerFieldUpdater<OBJECT> actual)
Create assertion for
AtomicIntegerFieldUpdater . |
static AtomicLongAssert |
assertThat(AtomicLong actual)
Create assertion for
AtomicLong . |
static AtomicLongArrayAssert |
assertThat(AtomicLongArray actual)
Create assertion for
AtomicLongArray . |
static <OBJECT> AtomicLongFieldUpdaterAssert<OBJECT> |
assertThat(AtomicLongFieldUpdater<OBJECT> actual)
Create assertion for
AtomicLongFieldUpdater . |
static <VALUE> AtomicMarkableReferenceAssert<VALUE> |
assertThat(AtomicMarkableReference<VALUE> actual)
Create assertion for
AtomicMarkableReference . |
static <VALUE> AtomicReferenceAssert<VALUE> |
assertThat(AtomicReference<VALUE> actual)
Create assertion for
AtomicReference . |
static <ELEMENT> AtomicReferenceArrayAssert<ELEMENT> |
assertThat(AtomicReferenceArray<ELEMENT> actual)
Create assertion for
AtomicReferenceArray . |
static <FIELD,OBJECT> |
assertThat(AtomicReferenceFieldUpdater<OBJECT,FIELD> actual)
Create assertion for
AtomicReferenceFieldUpdater . |
static <VALUE> AtomicStampedReferenceAssert<VALUE> |
assertThat(AtomicStampedReference<VALUE> actual)
Create assertion for
AtomicStampedReference . |
static AbstractBigDecimalAssert<?> |
assertThat(BigDecimal actual)
Creates a new instance of
. |
static AbstractBigIntegerAssert<?> |
assertThat(BigInteger 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 <RESULT> AbstractFutureAssert<?,? extends Future<? extends RESULT>,RESULT> |
assertThat(Future<RESULT> actual)
Create assertion for
Future . |
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 <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
allowing to navigate to any Iterable element
in order to perform assertions on it. |
static <T> AbstractIterableAssert<?,Iterable<? extends T>,T,ObjectAssert<T>> |
assertThat(Iterable<? extends T> actual)
Creates a new instance of
. |
static <T> AbstractIteratorAssert<?,T> |
assertThat(Iterator<? extends T> 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)
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>> |
assertThat(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>> |
assertThat(List<? extends T> 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 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 AbstractStringAssert<?> |
assertThat(String actual)
Creates a new instance of
. |
static AbstractCharSequenceAssert<?,? extends CharSequence> |
assertThat(StringBuffer actual)
Creates a new instance of
from a StringBuffer . |
static AbstractCharSequenceAssert<?,? extends CharSequence> |
assertThat(StringBuilder actual)
Creates a new instance of
from a StringBuilder . |
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 AbstractUriAssert<?> |
assertThat(URI actual)
Creates a new instance of
. |
static AbstractUrlAssert<?> |
assertThat(URL actual)
Creates a new instance of
. |
static AbstractThrowableAssert<?,? extends Throwable> |
assertThatCode(ThrowableAssert.ThrowingCallable shouldRaiseOrNotThrowable)
Allows to capture and then assert on a
Throwable . |
<T> AbstractObjectAssert<?,T> |
assertThatObject(T actual)
Creates a new instance of
for any object. |
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 AbstractThrowableAssert<?,? extends Throwable> |
assertThatThrownBy(ThrowableAssert.ThrowingCallable shouldRaiseThrowable,
String description,
Object... args)
Allows to capture and then assert on a
Throwable like assertThatThrownBy(ThrowingCallable) but this method
let you set the assertion description the same way you do with as(String, Object...) . |
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 Offset<BigDecimal> |
byLessThan(BigDecimal value)
Assertions entry point for BigDecimal
Offset to use with isCloseTo assertions. |
static Offset<BigInteger> |
byLessThan(BigInteger value)
Assertions entry point for BigInteger
Offset to use with isCloseTo assertions. |
static Offset<Byte> |
byLessThan(Byte value)
Assertions entry point for Byte
Offset to use with isCloseTo assertions. |
static Offset<Double> |
byLessThan(Double value)
Alias for
offset(Double) to use with isCloseTo assertions. |
static Offset<Float> |
byLessThan(Float value)
Alias for
offset(Float) to use with isCloseTo assertions. |
static Offset<Integer> |
byLessThan(Integer value)
Assertions entry point for Integer
Offset to use with isCloseTo assertions. |
static Offset<Long> |
byLessThan(Long value)
Assertions entry point for Long
Offset to use with isCloseTo assertions. |
static Offset<Short> |
byLessThan(Short value)
Assertions entry point for Short
Offset to use with isCloseTo assertions. |
static Throwable |
catchThrowable(ThrowableAssert.ThrowingCallable shouldRaiseThrowable)
Allows to catch an
Throwable more easily when used with Java 8 lambdas. |
static <THROWABLE extends Throwable> |
catchThrowableOfType(ThrowableAssert.ThrowingCallable shouldRaiseThrowable,
Class<THROWABLE> type)
Allows catching a
Throwable of a specific type. |
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 String |
contentOf(URL url)
Loads the text content of a URL with the default character set, so that it can be passed to
assertThat(String) . |
static String |
contentOf(URL url,
Charset charset)
Loads the text content of a URL, so that it can be passed to
assertThat(String) . |
static String |
contentOf(URL url,
String charsetName)
Loads the text content of a URL, so that it can be passed to
assertThat(String) . |
static <T> DoesNotHave<T> |
doesNotHave(Condition<? super T> condition)
Creates a new
. |
static <K,V> MapEntry<K,V> |
entry(K key,
V value)
Only delegate to
MapEntry.entry(Object, Object) 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)
Throws an
AssertionError with the given message. |
static void |
fail(String failureMessage,
Object... args)
Throws an
AssertionError with the given message built as String.format(String, Object...) . |
static void |
fail(String failureMessage,
Throwable realCause)
Throws an
AssertionError with the given message and with the Throwable that caused the failure. |
static void |
failBecauseExceptionWasNotThrown(Class<? extends Throwable> throwableClass)
Throws an
AssertionError with a message explaining that a Throwable of given class was expected to be thrown
but had not been. |
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 InFilter |
in(Object... values)
Create a
FilterOperator to use in filteredOn(String, FilterOperation) to express a filter keeping all Iterable elements whose property/field
value matches one of the given values. |
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 List<String> |
linesOf(URL url)
Loads the text content of a URL into a list of strings with the default charset, each string corresponding to a
line.
|
static List<String> |
linesOf(URL url,
Charset charset)
Loads the text content of a URL into a list of strings, each string corresponding to a line.
|
static List<String> |
linesOf(URL url,
String charsetName)
Loads the text content of a URL into a list of strings, each string corresponding to a line.
|
static <T> Not<T> |
not(Condition<? super T> condition)
Creates a new
. |
static NotFilter |
not(Object valueNotToMatch)
Create a
FilterOperator to use in filteredOn(String, FilterOperation) to express a filter keeping all Iterable elements whose property/field
value matches does not match the given value. |
static NotInFilter |
notIn(Object... valuesNotToMatch)
Create a
FilterOperator to use in filteredOn(String, FilterOperation) to express a filter keeping all Iterable elements whose property/field
value matches does not match any of the given values. |
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 |
setLenientDateParsing(boolean value)
Instead of using default strict date/time parsing, it is possible to use lenient parsing mode for default date
formats parser to interpret inputs that do not precisely match supported date formats (lenient parsing).
|
static void |
setMaxElementsForPrinting(int maxElementsForPrinting)
In error messages, sets the threshold for how many elements from one iterable/array/map will be included in the
in the description.
|
static void |
setMaxLengthForSingleLineDescription(int maxLengthForSingleLineDescription)
In error messages, sets the threshold when iterable/array formatting will on one line (if their String description
is less than this parameter) or it will be formatted with one element per line.
|
static void |
shouldHaveThrown(Class<? extends Throwable> throwableClass)
Throws an
AssertionError with a message explaining that a Throwable of given class was expected to be thrown
but had not been. |
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<BigInteger> |
within(BigInteger value)
Assertions entry point for BigInteger
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. |
static Percentage |
withinPercentage(Double value)
Assertions entry point for Double
Percentage to use with isCloseTo assertions for
percentages. |
static Percentage |
withinPercentage(Integer value)
Assertions entry point for Integer
Percentage to use with isCloseTo assertions for
percentages. |
static Percentage |
withinPercentage(Long value)
Assertions entry point for Long
Percentage to use with isCloseTo assertions for
percentages. |
public static AtomicBooleanAssert assertThat(AtomicBoolean actual)
AtomicBoolean
.actual
- the actual value.public static AtomicIntegerAssert assertThat(AtomicInteger actual)
AtomicInteger
.actual
- the actual value.public static AtomicIntegerArrayAssert assertThat(AtomicIntegerArray actual)
AtomicIntegerArray
.actual
- the actual value.public static <OBJECT> AtomicIntegerFieldUpdaterAssert<OBJECT> assertThat(AtomicIntegerFieldUpdater<OBJECT> actual)
AtomicIntegerFieldUpdater
.OBJECT
- the type of the object holding the updatable field.actual
- the actual value.public static AtomicLongAssert assertThat(AtomicLong actual)
AtomicLong
.actual
- the actual value.public static AtomicLongArrayAssert assertThat(AtomicLongArray actual)
AtomicLongArray
.actual
- the actual value.public static <OBJECT> AtomicLongFieldUpdaterAssert<OBJECT> assertThat(AtomicLongFieldUpdater<OBJECT> actual)
AtomicLongFieldUpdater
.OBJECT
- the type of the object holding the updatable field.actual
- the actual value.public static <VALUE> AtomicReferenceAssert<VALUE> assertThat(AtomicReference<VALUE> actual)
AtomicReference
.VALUE
- the type of the value contained in the AtomicReference
.actual
- the actual value.public static <ELEMENT> AtomicReferenceArrayAssert<ELEMENT> assertThat(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> assertThat(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> assertThat(AtomicMarkableReference<VALUE> actual)
AtomicMarkableReference
.VALUE
- the type of the value contained in the AtomicMarkableReference
.actual
- the actual value.public static <VALUE> AtomicStampedReferenceAssert<VALUE> assertThat(AtomicStampedReference<VALUE> actual)
AtomicStampedReference
.VALUE
- the type of the value contained in the AtomicStampedReference
.actual
- the actual value.public static AbstractBigDecimalAssert<?> assertThat(BigDecimal actual)
BigDecimalAssert
.actual
- the actual value.public static AbstractBigIntegerAssert<?> assertThat(BigInteger actual)
BigIntegerAssert
.actual
- the actual value.public static AbstractUriAssert<?> assertThat(URI actual)
UriAssert
.actual
- the actual value.public static AbstractUrlAssert<?> assertThat(URL actual)
UrlAssert
.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.T
- the type of the actual value.actual
- the actual value.public static <T> AbstractIterableAssert<?,Iterable<? extends T>,T,ObjectAssert<T>> assertThat(Iterable<? extends T> actual)
IterableAssert
.T
- the actual elements typeactual
- the actual value.public static <T> AbstractIteratorAssert<?,T> assertThat(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();
assertThat(bestBasketBallPlayers).hasNext() // Iterator assertion
.toIterable() // switch to Iterable assertions
.contains("Jordan", "Magic", "Lebron"); // Iterable assertion
T
- the type of elements.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 <RESULT> AbstractFutureAssert<?,? extends Future<? extends RESULT>,RESULT> assertThat(Future<RESULT> actual)
Future
.RESULT
- the type of the value contained in the Future
.actual
- the actual value.public 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<?,List<? extends T>,T,ObjectAssert<T>> assertThat(List<? extends T> actual)
ListAssert
.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> assertThat(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()
assertThat(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> assertThat(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()
assertThat(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> assertThat(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()
assertThat(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> assertThat(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()
assertThat(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 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
.T
- the actual typeactual
- 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.
T
- the generic type of the assert provided by the component.component
- the component that creates its own assertAssert
of the given componentpublic static <T> AbstractObjectArrayAssert<?,T> assertThat(T[] actual)
ObjectArrayAssert
.T
- the actual elements typeactual
- 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.
K
- the type of keys in the map.V
- the type of values in the map.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<?,? extends CharSequence> assertThat(StringBuilder actual)
CharSequenceAssert
from a StringBuilder
.actual
- the actual value.public static AbstractCharSequenceAssert<?,? extends CharSequence> assertThat(StringBuffer actual)
CharSequenceAssert
from a StringBuffer
.actual
- the actual value.public static AbstractStringAssert<?> assertThat(String actual)
StringAssert
.actual
- the actual value.public static AbstractDateAssert<?> assertThat(Date actual)
DateAssert
.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.
Java 8 example :
@Test
public void testException() {
assertThatThrownBy(() -> { throw new Exception("boom!"); }).isInstanceOf(Exception.class)
.hasMessageContaining("boom");
}
Java 7 example :
assertThatThrownBy(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.catchThrowable(ThrowableAssert.ThrowingCallable)
as shown below.
// assertion will fail but "display me" won't appear in the error
assertThatThrownBy(() -> {}).as("display me")
.isInstanceOf(Exception.class);
// assertion will fail AND "display me" will appear in the error
Throwable thrown = catchThrowable(() -> {});
assertThat(thrown).as("display me")
.isInstanceOf(Exception.class);
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> assertThatThrownBy(ThrowableAssert.ThrowingCallable shouldRaiseThrowable, String description, Object... args)
Throwable
like assertThatThrownBy(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]
assertThatThrownBy(() -> { 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
assertThatThrownBy(() -> {}).as("display me")
.isInstanceOf(Exception.class);
// assertion will fail AND "display me" will appear in the error message
assertThatThrownBy(() -> {}, "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 static AbstractThrowableAssert<?,? extends Throwable> assertThatCode(ThrowableAssert.ThrowingCallable shouldRaiseOrNotThrowable)
Throwable
.
Example :
ThrowingCallable callable = new ThrowingCallable() {
@Override
public void call() throws Throwable {
throw new Exception("boom!");
}
};
// assertion succeeds
assertThatCode(callable).isInstanceOf(Exception.class)
.hasMessageContaining("boom");
// assertion fails
assertThatCode(callable).doesNotThrowAnyException();
If the provided ThrowableAssert.ThrowingCallable
does not validate against next assertions, an error is immediately raised,
in that case the test description provided with as(String, Object...)
is not honored.catchThrowable(ThrowableAssert.ThrowingCallable)
as shown below.
ThrowingCallable doNothing = new ThrowingCallable() {
@Override
public void call() throws Throwable {
// do nothing
}
};
// assertion fails and "display me" appears in the assertion error
assertThatCode(doNothing).as("display me")
.isInstanceOf(Exception.class);
// assertion will fail AND "display me" will appear in the error
Throwable thrown = catchThrowable(doNothing);
assertThatCode(thrown).as("display me")
.isInstanceOf(Exception.class);
This method was not named assertThat
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 <T> AbstractObjectAssert<?,T> assertThatObject(T actual)
ObjectAssert
for any object.
This overload is useful, when an overloaded method of assertThat(...) takes precedence over the generic assertThat(Object)
.
Example:
Cast necessary because assertThat(List)
"forgets" actual type:
assertThat(new LinkedList<>(asList("abc"))).matches(list -> ((Deque<String>) list).getFirst().equals("abc"));
No cast needed, but also no additional list assertions:
assertThatObject(new LinkedList<>(asList("abc"))).matches(list -> list.getFirst().equals("abc"));
T
- the type of the actual value.actual
- the actual value.public static Throwable catchThrowable(ThrowableAssert.ThrowingCallable shouldRaiseThrowable)
Throwable
more easily when used with Java 8 lambdas.
This caught Throwable
can then be asserted.
Java 8 example:
@Test
public void testException() {
// when
Throwable thrown = catchThrowable(() -> { throw new Exception("boom!"); });
// then
assertThat(thrown).isInstanceOf(Exception.class)
.hasMessageContaining("boom");
}
Java 7 example:
@Test
public void testException() {
// when
Throwable thrown = catchThrowable(new ThrowingCallable() {
@Override
public void call() throws Exception {
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 <THROWABLE extends Throwable> THROWABLE catchThrowableOfType(ThrowableAssert.ThrowingCallable shouldRaiseThrowable, Class<THROWABLE> type)
Throwable
of a specific type.
A call is made to catchThrowable(ThrowingCallable)
, if no exception is thrown catchThrowableOfType
returns null,
otherwise it checks that the caught Throwable
has the specified type then casts it to it before returning it,
making it convenient to perform subtype-specific assertions on the result.
Example:
class CustomParseException extends Exception {
int line;
int column;
public CustomParseException(String msg, int l, int c) {
super(msg);
line = l;
column = c;
}
}
CustomParseException e = catchThrowableOfType(() -> { throw new CustomParseException("boom!", 1, 5); },
CustomParseException.class);
// assertions pass
assertThat(e).hasMessageContaining("boom");
assertThat(e.line).isEqualTo(1);
assertThat(e.column).isEqualTo(5);
// fails as CustomParseException is not a RuntimeException
catchThrowableOfType(() -> { throw new CustomParseException("boom!", 1, 5); },
RuntimeException.class);
THROWABLE
- the Throwable
type.shouldRaiseThrowable
- The lambda with the code that should raise the exception.type
- The type of exception that the code is expected to raise.null
if none was raised by the callable.catchThrowable(ThrowableAssert.ThrowingCallable)
public static void fail(String failureMessage)
AssertionError
with the given message.failureMessage
- error message.AssertionError
- with the given message.public static void fail(String failureMessage, Object... args)
AssertionError
with the given message built as String.format(String, Object...)
.failureMessage
- error message.args
- Arguments referenced by the format specifiers in the format string.AssertionError
- with the given built message.public static void fail(String failureMessage, Throwable realCause)
AssertionError
with the given message and with the Throwable
that caused the failure.failureMessage
- the description of the failed assertion. It can be null
.realCause
- cause of the error.AssertionError
- with the given message and with the Throwable
that caused the failure.public static void failBecauseExceptionWasNotThrown(Class<? extends Throwable> throwableClass)
AssertionError
with a message explaining that a Throwable
of given class was expected to be thrown
but had not been.
Assertions.shouldHaveThrown(Class)
can be used as a replacement.
throwableClass
- the Throwable class that was expected to be thrown.AssertionError
- with a message explaining that a Throwable
of given class was expected to be thrown but had
not been.public static void shouldHaveThrown(Class<? extends Throwable> throwableClass)
AssertionError
with a message explaining that a Throwable
of given class was expected to be thrown
but had not been.throwableClass
- the Throwable class that was expected to be thrown.AssertionError
- with a message explaining that a Throwable
of given class was expected to be thrown but had
not been.public static void setMaxLengthForSingleLineDescription(int maxLengthForSingleLineDescription)
The following array will be formatted on one line as its length < 80:
String[] greatBooks = array("A Game of Thrones", "The Lord of the Rings", "Assassin's Apprentice");
// formatted as:
["A Game of Thrones", "The Lord of the Rings", "Assassin's Apprentice"]
whereas this array is formatted on multiple lines (one element per line):
String[] greatBooks = array("A Game of Thrones", "The Lord of the Rings", "Assassin's Apprentice", "Guards! Guards! (Discworld)");
// formatted as:
["A Game of Thrones",
"The Lord of the Rings",
"Assassin's Apprentice",
"Guards! Guards! (Discworld)"]
maxLengthForSingleLineDescription
- the maximum length for an iterable/array to be displayed on one linepublic static void setMaxElementsForPrinting(int maxElementsForPrinting)
3
.
The following array will be formatted entirely as it's length is <= 3:
String[] greatBooks = array("A Game of Thrones", "The Lord of the Rings", "Assassin's Apprentice");
// formatted as:
["A Game of Thrones", "The Lord of the Rings", "Assassin's Apprentice"]
whereas this array is formatted only with it's first 3 elements, followed by ...
:
String[] greatBooks = array("A Game of Thrones", "The Lord of the Rings", "Assassin's Apprentice", "Guards! Guards!");
// formatted as:
["A Game of Thrones", "The Lord of the Rings", "Assassin's Apprentice", ...]
maxElementsForPrinting
- the maximum elements that would be printed from one iterable/array/mappublic 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");
T
- the type of value to extract.propertyName
- the name of the property to be read from the elements of a Iterable
. It may be a nested
property (e.g. "address.street.number").propertyType
- the type of property to extractProperties
.NullPointerException
- if the given property name is null
.IllegalArgumentException
- if the given property name is empty.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");
propertyName
- the name of the property to be read from the elements of a Iterable
. It may be a nested
property (e.g. "address.street.number").Properties
.NullPointerException
- if the given property name is null
.IllegalArgumentException
- if the given property name is empty.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...)
AbstractObjectAssert.isEqualToComparingFieldByField(Object)
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(Object, Object)
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<Ring, TolkienCharacter> ringBearers = ... // init omitted
assertThat(ringBearers).contains(entry(oneRing, frodo), entry(nenya, galadriel));
K
- the type of keys in the map.V
- the type of values in the map.key
- the key of the entry to create.value
- the value of the entry to create.MapEntry
.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));
index
- the value of the index.Index
.IllegalArgumentException
- if the given value is negative.public static Offset<Double> offset(Double value)
Offset
.
Typical usage :
assertThat(8.1).isEqualTo(8.0, offset(0.1));
value
- the value of the offset.Offset
.NullPointerException
- if the given value is null
.IllegalArgumentException
- if the given value is negative.public static Offset<Float> offset(Float value)
Offset
.
Typical usage :
assertThat(8.2f).isCloseTo(8.0f, offset(0.2f));
value
- the value of the offset.Offset
.NullPointerException
- if the given value is null
.IllegalArgumentException
- if the given value is negative.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));
value
- the value of the offset.Offset
.NullPointerException
- if the given value is null
.IllegalArgumentException
- if the given value is negative.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));
value
- the value of the offset.Offset
.NullPointerException
- if the given value is null
.IllegalArgumentException
- if the given value is negative.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));
value
- the value of the offset.Offset
.NullPointerException
- if the given value is null
.IllegalArgumentException
- if the given value is negative.public static Offset<BigInteger> within(BigInteger value)
Offset
to use with isCloseTo assertions.
Typical usage :
assertThat(BigInteger.TEN).isCloseTo(new BigInteger("11"), within(new BigInteger("2")));
value
- the value of the offset.Offset
.NullPointerException
- if the given value is null
.IllegalArgumentException
- if the given value is negative.public static Offset<Byte> within(Byte value)
Offset
to use with isCloseTo assertions.
Typical usage :
assertThat((byte) 10).isCloseTo((byte) 11, within((byte) 1));
value
- the value of the offset.Offset
.NullPointerException
- if the given value is null
.IllegalArgumentException
- if the given value is negative.public static Offset<Integer> within(Integer value)
Offset
to use with isCloseTo assertions.
Typical usage :
assertThat(10).isCloseTo(11, within(1));
value
- the value of the offset.Offset
.NullPointerException
- if the given value is null
.IllegalArgumentException
- if the given value is negative.public static Offset<Short> within(Short value)
Offset
to use with isCloseTo assertions.
Typical usage :
assertThat(10).isCloseTo(11, within(1));
value
- the value of the offset.Offset
.NullPointerException
- if the given value is null
.IllegalArgumentException
- if the given value is negative.public static Offset<Long> within(Long value)
Offset
to use with isCloseTo assertions.
Typical usage :
assertThat(5l).isCloseTo(7l, within(2l));
value
- the value of the offset.Offset
.NullPointerException
- if the given value is null
.IllegalArgumentException
- if the given value is negative.public static Offset<Double> byLessThan(Double value)
offset(Double)
to use with isCloseTo assertions.
Typical usage :
assertThat(8.1).isCloseTo(8.0, byLessThan(0.1));
value
- the value of the offset.Offset
.NullPointerException
- if the given value is null
.IllegalArgumentException
- if the given value is negative.public static Offset<Float> byLessThan(Float value)
offset(Float)
to use with isCloseTo assertions.
Typical usage :
assertThat(8.2f).isCloseTo(8.0f, byLessThan(0.2f));
value
- the value of the offset.Offset
.NullPointerException
- if the given value is null
.IllegalArgumentException
- if the given value is negative.public static Offset<BigDecimal> byLessThan(BigDecimal value)
Offset
to use with isCloseTo assertions.
Typical usage :
assertThat(BigDecimal.TEN).isCloseTo(new BigDecimal("10.5"), byLessThan(BigDecimal.ONE));
value
- the value of the offset.Offset
.NullPointerException
- if the given value is null
.IllegalArgumentException
- if the given value is negative.public static Offset<BigInteger> byLessThan(BigInteger value)
Offset
to use with isCloseTo assertions.
Typical usage :
assertThat(BigInteger.TEN).isCloseTo(new BigInteger("11"), byLessThan(new BigInteger("2")));
value
- the value of the offset.Offset
.NullPointerException
- if the given value is null
.IllegalArgumentException
- if the given value is negative.public static Offset<Byte> byLessThan(Byte value)
Offset
to use with isCloseTo assertions.
Typical usage :
assertThat((byte) 10).isCloseTo((byte) 11, byLessThan((byte) 1));
value
- the allowed offsetOffset
.public static Offset<Integer> byLessThan(Integer value)
Offset
to use with isCloseTo assertions.
Typical usage :
assertThat(10).isCloseTo(11, byLessThan(1));
value
- the value of the offset.Offset
.NullPointerException
- if the given value is null
.IllegalArgumentException
- if the given value is negative.public static Offset<Short> byLessThan(Short value)
Offset
to use with isCloseTo assertions.
Typical usage :
assertThat(10).isCloseTo(11, byLessThan(1));
value
- the value of the offset.Offset
.NullPointerException
- if the given value is null
.IllegalArgumentException
- if the given value is negative.public static Offset<Long> byLessThan(Long value)
Offset
to use with isCloseTo assertions.
Typical usage :
assertThat(5l).isCloseTo(7l, byLessThan(2l));
value
- the value of the offset.Offset
.NullPointerException
- if the given value is null
.IllegalArgumentException
- if the given value is negative.public static Percentage withinPercentage(Double value)
Percentage
to use with isCloseTo assertions for
percentages.
Typical usage :
assertThat(11.0).isCloseTo(10.0, withinPercentage(10.0));
value
- the value of the percentage.Percentage
.NullPointerException
- if the given value is null
.IllegalArgumentException
- if the given value is negative.public static Percentage withinPercentage(Integer value)
Percentage
to use with isCloseTo assertions for
percentages.
Typical usage :
assertThat(11).isCloseTo(10, withinPercentage(10));
value
- the value of the percentage.Percentage
.NullPointerException
- if the given value is null
.IllegalArgumentException
- if the given value is negative.public static Percentage withinPercentage(Long value)
Percentage
to use with isCloseTo assertions for
percentages.
Typical usage :
assertThat(11L).isCloseTo(10L, withinPercentage(10L));
value
- the value of the percentage.Percentage
.NullPointerException
- if the given value is null
.IllegalArgumentException
- if the given value is negative.@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));
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> 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
.T
- the type of object the given condition accept.condition
- the condition to inverse.public static <T> Not<T> not(Condition<? super T> condition)
Not
.T
- the type of object the given condition accept.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);
E
- the array elements type.array
- the array to filter.Filters
.NullPointerException
- if the given array is null
.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);
E
- the iterable elements type.iterableToFilter
- the Iterable
to filter.Filters
.NullPointerException
- if the given iterable is null
.public static InFilter in(Object... values)
FilterOperator
to use in filteredOn(String, FilterOperation)
to express a filter keeping all Iterable elements whose property/field
value matches one of the given values.
As often, an example helps:
Employee yoda = new Employee(1L, new Name("Yoda"), 800);
Employee obiwan = new Employee(2L, new Name("Obiwan"), 800);
Employee luke = new Employee(3L, new Name("Luke", "Skywalker"), 26);
Employee noname = new Employee(4L, null, 50);
List<Employee> employees = newArrayList(yoda, luke, obiwan, noname);
assertThat(employees).filteredOn("age", in(800, 26))
.containsOnly(yoda, obiwan, luke);
values
- values to match (one match is sufficient)public static NotInFilter notIn(Object... valuesNotToMatch)
FilterOperator
to use in filteredOn(String, FilterOperation)
to express a filter keeping all Iterable elements whose property/field
value matches does not match any of the given values.
As often, an example helps:
Employee yoda = new Employee(1L, new Name("Yoda"), 800);
Employee obiwan = new Employee(2L, new Name("Obiwan"), 800);
Employee luke = new Employee(3L, new Name("Luke", "Skywalker"), 26);
Employee noname = new Employee(4L, null, 50);
List<Employee> employees = newArrayList(yoda, luke, obiwan, noname);
assertThat(employees).filteredOn("age", notIn(800, 50))
.containsOnly(luke);
valuesNotToMatch
- values not to match (none of the values must match)public static NotFilter not(Object valueNotToMatch)
FilterOperator
to use in filteredOn(String, FilterOperation)
to express a filter keeping all Iterable elements whose property/field
value matches does not match the given value.
As often, an example helps:
Employee yoda = new Employee(1L, new Name("Yoda"), 800);
Employee obiwan = new Employee(2L, new Name("Obiwan"), 800);
Employee luke = new Employee(3L, new Name("Luke", "Skywalker"), 26);
Employee noname = new Employee(4L, null, 50);
List<Employee> employees = newArrayList(yoda, luke, obiwan, noname);
assertThat(employees).filteredOn("age", not(800))
.containsOnly(luke, noname);
valueNotToMatch
- the value not to matchpublic 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
.UncheckedIOException
- 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.UncheckedIOException
- 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.UncheckedIOException
- if an I/O exception occurs.public static List<String> linesOf(File file)
file
- the file.NullPointerException
- if the given charset is null
.UncheckedIOException
- 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
.UncheckedIOException
- 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
.UncheckedIOException
- if an I/O exception occurs.public static String contentOf(URL url, Charset charset)
assertThat(String)
.
Note that this will load the entire contents in memory.
url
- the URL.charset
- the character set to use.NullPointerException
- if the given charset is null
.UncheckedIOException
- if an I/O exception occurs.public static String contentOf(URL url, String charsetName)
assertThat(String)
.
Note that this will load the entire contents in memory.
url
- the URL.charsetName
- the name of the character set to use.IllegalArgumentException
- if the given character set is not supported on this platform.UncheckedIOException
- if an I/O exception occurs.public static String contentOf(URL url)
assertThat(String)
.
Note that this will load the entire file in memory; for larger files.
url
- the URL.UncheckedIOException
- if an I/O exception occurs.public static List<String> linesOf(URL url)
url
- the URL.NullPointerException
- if the given charset is null
.UncheckedIOException
- if an I/O exception occurs.public static List<String> linesOf(URL url, Charset charset)
url
- the URL.charset
- the character set to use.NullPointerException
- if the given charset is null
.UncheckedIOException
- if an I/O exception occurs.public static List<String> linesOf(URL url, String charsetName)
url
- the URL.charsetName
- the name of the character set to use.NullPointerException
- if the given charset is null
.UncheckedIOException
- if an I/O exception occurs.public static void setLenientDateParsing(boolean value)
With strict parsing, inputs must match exactly date/time format.
Example:
final Date date = Dates.parse("2001-02-03");
final Date dateTime = parseDatetime("2001-02-03T04:05:06");
final Date dateTimeWithMs = parseDatetimeWithMs("2001-02-03T04:05:06.700");
Assertions.setLenientDateParsing(true);
// assertions will pass
assertThat(date).isEqualTo("2001-01-34");
assertThat(date).isEqualTo("2001-02-02T24:00:00");
assertThat(date).isEqualTo("2001-02-04T-24:00:00.000");
assertThat(dateTime).isEqualTo("2001-02-03T04:05:05.1000");
assertThat(dateTime).isEqualTo("2001-02-03T04:04:66");
assertThat(dateTimeWithMs).isEqualTo("2001-02-03T04:05:07.-300");
// assertions will fail
assertThat(date).hasSameTimeAs("2001-02-04"); // different date
assertThat(dateTime).hasSameTimeAs("2001-02-03 04:05:06"); // leniency does not help here
To revert to default strict date parsing, call setLenientDateParsing(false)
.value
- whether lenient parsing mode should be enabled or notpublic 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)
Beware that AssertJ will use the newly registered format for all remaining Date assertions in the test suite
To revert to default formats only, call 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)
Beware that AssertJ will use the newly registered format for all remaining Date assertions in the test suite
To revert to default formats only, call 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()
Beware that the default formats are expressed in the current local timezone.
Defaults date format are:
yyyy-MM-dd'T'HH:mm:ss.SSS
yyyy-MM-dd HH:mm:ss.SSS
(for Timestamp
String representation support)yyyy-MM-dd'T'HH:mm:ss
yyyy-MM-dd
Example of valid string date representations:
2003-04-26T03:01:02.999
2003-04-26 03:01:02.999
2003-04-26T13:01:02
2003-04-26
Copyright © 2014–2019 AssertJ. All rights reserved.