org.assertj.core.api
Class Assertions

java.lang.Object
  extended by org.assertj.core.api.Assertions
Direct Known Subclasses:
BDDAssertions

public class Assertions
extends Object

Entry point for assertion methods for different data types. Each method in this class is a static factory for the type-specific assertion objects. The purpose of this class is to make test code more readable.

For example:


 int removed = employees.removeFired();
 assertThat(removed).isZero();

 List<Employee> newEmployees = employees.hired(TODAY);
 assertThat(newEmployees).hasSize(6);
 

Author:
Alex Ruiz, Yvonne Wang, David DIDIER, Ted Young, Joel Costigliola, Matthieu Baechler, Mikhail Mazursky, Nicolas François, Julien Meddah, William Delanoue

Constructor Summary
protected Assertions()
          Creates a new Assertions.
 
Method Summary
static
<T> Condition<T>
allOf(Condition<? super T>... conditions)
          Creates a new AllOf
static
<T> Condition<T>
allOf(Iterable<? extends Condition<? super T>> conditions)
          Creates a new AllOf
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 AnyOf
static AbstractBigDecimalAssert<?> assertThat(BigDecimal actual)
          Creates a new instance of BigDecimalAssert.
static AbstractBooleanAssert<?> assertThat(boolean actual)
          Creates a new instance of BooleanAssert.
static AbstractBooleanAssert<?> assertThat(Boolean actual)
          Creates a new instance of BooleanAssert.
static AbstractBooleanArrayAssert<?> assertThat(boolean[] actual)
          Creates a new instance of BooleanArrayAssert.
static AbstractByteAssert<?> assertThat(byte actual)
          Creates a new instance of ByteAssert.
static AbstractByteAssert<?> assertThat(Byte actual)
          Creates a new instance of ByteAssert.
static AbstractByteArrayAssert<?> assertThat(byte[] actual)
          Creates a new instance of ByteArrayAssert.
static AbstractCharacterAssert<?> assertThat(char actual)
          Creates a new instance of CharacterAssert.
static AbstractCharArrayAssert<?> assertThat(char[] actual)
          Creates a new instance of CharArrayAssert.
static AbstractCharacterAssert<?> assertThat(Character actual)
          Creates a new instance of CharacterAssert.
static AbstractCharSequenceAssert<?,? extends CharSequence> assertThat(CharSequence actual)
          Creates a new instance of CharSequenceAssert.
static AbstractClassAssert<?> assertThat(Class<?> actual)
          Creates a new instance of ClassAssert
static AbstractDateAssert<?> assertThat(Date actual)
          Creates a new instance of DateAssert.
static AbstractDoubleAssert<?> assertThat(double actual)
          Creates a new instance of DoubleAssert.
static AbstractDoubleAssert<?> assertThat(Double actual)
          Creates a new instance of DoubleAssert.
static AbstractDoubleArrayAssert<?> assertThat(double[] actual)
          Creates a new instance of DoubleArrayAssert.
static AbstractFileAssert<?> assertThat(File actual)
          Creates a new instance of FileAssert.
static AbstractFloatAssert<?> assertThat(float actual)
          Creates a new instance of FloatAssert.
static AbstractFloatAssert<?> assertThat(Float actual)
          Creates a new instance of FloatAssert.
static AbstractFloatArrayAssert<?> assertThat(float[] actual)
          Creates a new instance of FloatArrayAssert.
static AbstractInputStreamAssert<?,? extends InputStream> assertThat(InputStream actual)
          Creates a new instance of InputStreamAssert.
static AbstractIntegerAssert<?> assertThat(int actual)
          Creates a new instance of IntegerAssert.
static AbstractIntArrayAssert<?> assertThat(int[] actual)
          Creates a new instance of IntArrayAssert.
static AbstractIntegerAssert<?> assertThat(Integer actual)
          Creates a new instance of IntegerAssert.
static
<T> AbstractIterableAssert<?,? extends Iterable<? extends T>,T>
assertThat(Iterable<? extends T> actual)
          Creates a new instance of IterableAssert.
static
<T> AbstractIterableAssert<?,? extends Iterable<? extends T>,T>
assertThat(Iterator<? extends T> actual)
          Creates a new instance of IterableAssert.
static
<T> AbstractListAssert<?,? extends List<? extends T>,T>
assertThat(List<? extends T> actual)
          Creates a new instance of ListAssert.
static AbstractLongAssert<?> assertThat(long actual)
          Creates a new instance of LongAssert.
static AbstractLongAssert<?> assertThat(Long actual)
          Creates a new instance of LongAssert.
static AbstractLongArrayAssert<?> assertThat(long[] actual)
          Creates a new instance of LongArrayAssert.
static
<K,V> AbstractMapAssert<?,? extends Map<K,V>,K,V>
assertThat(Map<K,V> actual)
          Creates a new instance of MapAssert.
static AbstractShortAssert<?> assertThat(short actual)
          Creates a new instance of ShortAssert.
static AbstractShortAssert<?> assertThat(Short actual)
          Creates a new instance of ShortAssert.
static AbstractShortArrayAssert<?> assertThat(short[] actual)
          Creates a new instance of ShortArrayAssert.
static AbstractCharSequenceAssert<?,String> assertThat(String actual)
          Creates a new instance of StringAssert.
static
<T> AbstractObjectAssert<?,T>
assertThat(T actual)
          Creates a new instance of ObjectAssert.
static
<T extends Comparable<? super T>>
AbstractComparableAssert<?,T>
assertThat(T actual)
          Creates a new instance of GenericComparableAssert with standard comparison semantics.
static
<T extends AssertDelegateTarget>
T
assertThat(T assertion)
          Returns the given assertion.
static
<T> AbstractObjectArrayAssert<?,T>
assertThat(T[] actual)
          Creates a new instance of ObjectArrayAssert.
static AbstractThrowableAssert<?,? extends Throwable> assertThat(Throwable actual)
          Creates a new instance of ThrowableAssert.
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 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 MapEntry entry(Object key, Object 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)
          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 setAllowExtractingPrivateFields(boolean allowExtractingPrivateFields)
          Globally set whether IterableAssert#extracting(String) and ObjectArrayAssert#extracting(String) 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 Tuple tuple(Object... values)
          Utility method to build nicely a Tuple when working with AbstractIterableAssert.extracting(String...) or AbstractObjectArrayAssert.extracting(String...)
static void useDateFormat(DateFormat userCustomDateFormat)
          Deprecated. : use registerCustomDateFormat(java.text.DateFormat) instead.
static void useDateFormat(String userCustomDateFormatPattern)
          Deprecated. : use registerCustomDateFormat(String) instead.
static void useDefaultDateFormats()
          Deprecated. use useDefaultDateFormatsOnly() instead.
static void useDefaultDateFormatsOnly()
          Remove all registered custom date formats => use only the defaults date formats to parse string as date.
static void useIsoDateFormat()
          Deprecated. use useDefaultDateFormatsOnly() instead, it includes this format.
static void useIsoDateTimeFormat()
          Deprecated. use useDefaultDateFormatsOnly() instead, it includes this format.
static void useIsoDateTimeWithMsFormat()
          Deprecated. use useDefaultDateFormatsOnly() instead, it includes this format.
static Offset<BigDecimal> within(BigDecimal value)
          Assertions entry point for BigDecimal 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.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Assertions

protected Assertions()
Creates a new Assertions.

Method Detail

assertThat

public static AbstractBigDecimalAssert<?> assertThat(BigDecimal actual)
Creates a new instance of BigDecimalAssert.

Parameters:
actual - the actual value.
Returns:
the created assertion object.

assertThat

public static AbstractBooleanAssert<?> assertThat(boolean actual)
Creates a new instance of BooleanAssert.

Parameters:
actual - the actual value.
Returns:
the created assertion object.

assertThat

public static AbstractBooleanAssert<?> assertThat(Boolean actual)
Creates a new instance of BooleanAssert.

Parameters:
actual - the actual value.
Returns:
the created assertion object.

assertThat

public static AbstractBooleanArrayAssert<?> assertThat(boolean[] actual)
Creates a new instance of BooleanArrayAssert.

Parameters:
actual - the actual value.
Returns:
the created assertion object.

assertThat

public static AbstractByteAssert<?> assertThat(byte actual)
Creates a new instance of ByteAssert.

Parameters:
actual - the actual value.
Returns:
the created assertion object.

assertThat

public static AbstractByteAssert<?> assertThat(Byte actual)
Creates a new instance of ByteAssert.

Parameters:
actual - the actual value.
Returns:
the created assertion object.

assertThat

public static AbstractByteArrayAssert<?> assertThat(byte[] actual)
Creates a new instance of ByteArrayAssert.

Parameters:
actual - the actual value.
Returns:
the created assertion object.

assertThat

public static AbstractCharacterAssert<?> assertThat(char actual)
Creates a new instance of CharacterAssert.

Parameters:
actual - the actual value.
Returns:
the created assertion object.

assertThat

public static AbstractCharArrayAssert<?> assertThat(char[] actual)
Creates a new instance of CharArrayAssert.

Parameters:
actual - the actual value.
Returns:
the created assertion object.

assertThat

public static AbstractCharacterAssert<?> assertThat(Character actual)
Creates a new instance of CharacterAssert.

Parameters:
actual - the actual value.
Returns:
the created assertion object.

assertThat

public static AbstractClassAssert<?> assertThat(Class<?> actual)
Creates a new instance of ClassAssert

Parameters:
actual - the actual value.
Returns:
the created assertion object.

assertThat

public static <T extends Comparable<? super T>> AbstractComparableAssert<?,T> assertThat(T actual)
Creates a new instance of GenericComparableAssert with standard comparison semantics.

Parameters:
actual - the actual value.
Returns:
the created assertion object.

assertThat

public static <T> AbstractIterableAssert<?,? extends Iterable<? extends T>,T> assertThat(Iterable<? extends T> actual)
Creates a new instance of IterableAssert.

Parameters:
actual - the actual value.
Returns:
the created assertion object.

assertThat

public static <T> AbstractIterableAssert<?,? extends Iterable<? extends T>,T> assertThat(Iterator<? extends T> actual)
Creates a new instance of IterableAssert.

Be aware that calls to most methods on returned IterableAssert will consume Iterator so it won't be possible to iterate over it again. Calling multiple methods on returned IterableAssert is safe as Iterator's elements are cached by IterableAssert first time Iterator is consumed.

Parameters:
actual - the actual value.
Returns:
the created assertion object.

assertThat

public static AbstractDoubleAssert<?> assertThat(double actual)
Creates a new instance of DoubleAssert.

Parameters:
actual - the actual value.
Returns:
the created assertion object.

assertThat

public static AbstractDoubleAssert<?> assertThat(Double actual)
Creates a new instance of DoubleAssert.

Parameters:
actual - the actual value.
Returns:
the created assertion object.

assertThat

public static AbstractDoubleArrayAssert<?> assertThat(double[] actual)
Creates a new instance of DoubleArrayAssert.

Parameters:
actual - the actual value.
Returns:
the created assertion object.

assertThat

public static AbstractFileAssert<?> assertThat(File actual)
Creates a new instance of FileAssert.

Parameters:
actual - the actual value.
Returns:
the created assertion object.

assertThat

public static AbstractInputStreamAssert<?,? extends InputStream> assertThat(InputStream actual)
Creates a new instance of InputStreamAssert.

Parameters:
actual - the actual value.
Returns:
the created assertion object.

assertThat

public static AbstractFloatAssert<?> assertThat(float actual)
Creates a new instance of FloatAssert.

Parameters:
actual - the actual value.
Returns:
the created assertion object.

assertThat

public static AbstractFloatAssert<?> assertThat(Float actual)
Creates a new instance of FloatAssert.

Parameters:
actual - the actual value.
Returns:
the created assertion object.

assertThat

public static AbstractFloatArrayAssert<?> assertThat(float[] actual)
Creates a new instance of FloatArrayAssert.

Parameters:
actual - the actual value.
Returns:
the created assertion object.

assertThat

public static AbstractIntegerAssert<?> assertThat(int actual)
Creates a new instance of IntegerAssert.

Parameters:
actual - the actual value.
Returns:
the created assertion object.

assertThat

public static AbstractIntArrayAssert<?> assertThat(int[] actual)
Creates a new instance of IntArrayAssert.

Parameters:
actual - the actual value.
Returns:
the created assertion object.

assertThat

public static AbstractIntegerAssert<?> assertThat(Integer actual)
Creates a new instance of IntegerAssert.

Parameters:
actual - the actual value.
Returns:
the created assertion object.

assertThat

public static <T> AbstractListAssert<?,? extends List<? extends T>,T> assertThat(List<? extends T> actual)
Creates a new instance of ListAssert.

Parameters:
actual - the actual value.
Returns:
the created assertion object.

assertThat

public static AbstractLongAssert<?> assertThat(long actual)
Creates a new instance of LongAssert.

Parameters:
actual - the actual value.
Returns:
the created assertion object.

assertThat

public static AbstractLongAssert<?> assertThat(Long actual)
Creates a new instance of LongAssert.

Parameters:
actual - the actual value.
Returns:
the created assertion object.

assertThat

public static AbstractLongArrayAssert<?> assertThat(long[] actual)
Creates a new instance of LongArrayAssert.

Parameters:
actual - the actual value.
Returns:
the created assertion object.

assertThat

public static <T> AbstractObjectAssert<?,T> assertThat(T actual)
Creates a new instance of ObjectAssert.

Parameters:
actual - the actual value.
Returns:
the created assertion object.

assertThat

public static <T extends AssertDelegateTarget> T assertThat(T assertion)
Returns the given assertion. This method improves code readability by surrounding the given assertion with 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();
 }
 

Type Parameters:
T - the generic type of the user-defined assert.
Parameters:
assertion - the assertion to return.
Returns:
the given assertion.

assertThat

public static <T> AbstractObjectArrayAssert<?,T> assertThat(T[] actual)
Creates a new instance of ObjectArrayAssert.

Parameters:
actual - the actual value.
Returns:
the created assertion object.

assertThat

public static <K,V> AbstractMapAssert<?,? extends Map<K,V>,K,V> assertThat(Map<K,V> actual)
Creates a new instance of MapAssert.

Parameters:
actual - the actual value.
Returns:
the created assertion object.

assertThat

public static AbstractShortAssert<?> assertThat(short actual)
Creates a new instance of ShortAssert.

Parameters:
actual - the actual value.
Returns:
the created assertion object.

assertThat

public static AbstractShortAssert<?> assertThat(Short actual)
Creates a new instance of ShortAssert.

Parameters:
actual - the actual value.
Returns:
the created assertion object.

assertThat

public static AbstractShortArrayAssert<?> assertThat(short[] actual)
Creates a new instance of ShortArrayAssert.

Parameters:
actual - the actual value.
Returns:
the created assertion object.

assertThat

public static AbstractCharSequenceAssert<?,? extends CharSequence> assertThat(CharSequence actual)
Creates a new instance of CharSequenceAssert.

Parameters:
actual - the actual value.
Returns:
the created assertion object.

assertThat

public static AbstractCharSequenceAssert<?,String> assertThat(String actual)
Creates a new instance of StringAssert.

Parameters:
actual - the actual value.
Returns:
the created assertion object.

assertThat

public static AbstractDateAssert<?> assertThat(Date actual)
Creates a new instance of DateAssert.

Parameters:
actual - the actual value.
Returns:
the created assertion object.

assertThat

public static AbstractThrowableAssert<?,? extends Throwable> assertThat(Throwable actual)
Creates a new instance of ThrowableAssert.

Parameters:
actual - the actual value.
Returns:
the created assertion Throwable.

setRemoveAssertJRelatedElementsFromStackTrace

public 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).


fail

public 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).


fail

public 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).


failBecauseExceptionWasNotThrown

public 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).


extractProperty

public 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).

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");
 


extractProperty

public 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).

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");
 


tuple

public static Tuple tuple(Object... values)
Utility method to build nicely a Tuple when working with AbstractIterableAssert.extracting(String...) or AbstractObjectArrayAssert.extracting(String...)

Parameters:
values - the values stored in the Tuple
Returns:
the built Tuple

setAllowExtractingPrivateFields

public static void setAllowExtractingPrivateFields(boolean allowExtractingPrivateFields)
Globally set whether IterableAssert#extracting(String) and ObjectArrayAssert#extracting(String) should be allowed to extract private fields, if not and they try it fails with exception.

Parameters:
allowExtractingPrivateFields - allow private fields extraction. Default true.

entry

public static MapEntry entry(Object key,
                             Object 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).

Typical usage is to call entry in MapAssert contains assertion, see examples below :

 assertThat(ringBearers).contains(entry(oneRing, frodo), entry(nenya, galadriel));
 


atIndex

public 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).

Typical usage :

 List<Ring> elvesRings = newArrayList(vilya, nenya, narya);
 assertThat(elvesRings).contains(vilya, atIndex(0)).contains(nenya, atIndex(1)).contains(narya, atIndex(2));
 


offset

public static Offset<Double> offset(Double value)
Assertions entry point for double Offset.

Typical usage :

 assertThat(8.1).isEqualTo(8.0, offset(0.1));
 


offset

public static Offset<Float> offset(Float value)
Assertions entry point for float Offset.

Typical usage :

 assertThat(8.2f).isCloseTo(8.0f, offset(0.2f));
 


within

public static Offset<Double> within(Double value)
Alias for offset(Double) to use with isCloseTo assertions.

Typical usage :

 assertThat(8.1).isCloseTo(8.0, within(0.1));
 


within

public static Offset<Float> within(Float value)
Alias for offset(Float) to use with isCloseTo assertions.

Typical usage :

 assertThat(8.2f).isCloseTo(8.0f, within(0.2f));
 


within

public static Offset<BigDecimal> within(BigDecimal value)
Assertions entry point for BigDecimal Offset to use with isCloseTo assertions.

Typical usage :

 assertThat(BigDecimal.TEN).isCloseTo(new BigDecimal("10.5"), within(BigDecimal.ONE));
 


allOf

public static <T> Condition<T> allOf(Condition<? super T>... conditions)
Creates a new AllOf

Type Parameters:
T - the type of object the given condition accept.
Parameters:
conditions - the conditions to evaluate.
Returns:
the created AnyOf.
Throws:
NullPointerException - if the given array is null.
NullPointerException - if any of the elements in the given array is null.

allOf

public static <T> Condition<T> allOf(Iterable<? extends Condition<? super T>> conditions)
Creates a new AllOf

Type Parameters:
T - the type of object the given condition accept.
Parameters:
conditions - the conditions to evaluate.
Returns:
the created AnyOf.
Throws:
NullPointerException - if the given iterable is null.
NullPointerException - if any of the elements in the given iterable is null.

anyOf

public 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).

Typical usage (jedi and sith are Condition) :

 assertThat("Vader").is(anyOf(jedi, sith));
 


anyOf

public static <T> Condition<T> anyOf(Iterable<? extends Condition<? super T>> conditions)
Creates a new AnyOf

Type Parameters:
T - the type of object the given condition accept.
Parameters:
conditions - the conditions to evaluate.
Returns:
the created AnyOf.
Throws:
NullPointerException - if the given iterable is null.
NullPointerException - if any of the elements in the given iterable is null.

doesNotHave

public static <T> DoesNotHave<T> doesNotHave(Condition<? super T> condition)
Creates a new DoesNotHave.

Parameters:
condition - the condition to inverse.
Returns:
The Not condition created.

not

public static <T> Not<T> not(Condition<? super T> condition)
Creates a new Not.

Parameters:
condition - the condition to inverse.
Returns:
The Not condition created.

filter

public 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).

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);
 


filter

public 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).

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);
 


contentOf

public static String contentOf(File file,
                               Charset charset)
Loads the text content of a file, so that it can be passed to 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).

Parameters:
file - the file.
charset - the character set to use.
Returns:
the content of the file.
Throws:
NullPointerException - if the given charset is null.
FilesException - if an I/O exception occurs.

contentOf

public static String contentOf(File file,
                               String charsetName)
Loads the text content of a file, so that it can be passed to 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).

Parameters:
file - the file.
charsetName - the name of the character set to use.
Returns:
the content of the file.
Throws:
IllegalArgumentException - if the given character set is not supported on this platform.
FilesException - if an I/O exception occurs.

contentOf

public 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).

Note that this will load the entire file in memory; for larger files, there might be a more efficient alternative with assertThat(File).

Parameters:
file - the file.
Returns:
the content of the file.
Throws:
FilesException - if an I/O exception occurs.

linesOf

public 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. The line endings are either \n, \r or \r\n.

Parameters:
file - the file.
Returns:
the content of the file.
Throws:
NullPointerException - if the given charset is null.
FilesException - if an I/O exception occurs.

linesOf

public 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. The line endings are either \n, \r or \r\n.

Parameters:
file - the file.
charset - the character set to use.
Returns:
the content of the file.
Throws:
NullPointerException - if the given charset is null.
FilesException - if an I/O exception occurs.

linesOf

public 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. The line endings are either \n, \r or \r\n.

Parameters:
file - the file.
charsetName - the name of the character set to use.
Returns:
the content of the file.
Throws:
NullPointerException - if the given charset is null.
FilesException - if an I/O exception occurs.

registerCustomDateFormat

public 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).

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 :

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");
 

Parameters:
userCustomDateFormat - the new Date format used for String based Date assertions.

registerCustomDateFormat

public 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).

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 :

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");
 

Parameters:
userCustomDateFormatPattern - the new Date format pattern used for String based Date assertions.

useDefaultDateFormatsOnly

public static void useDefaultDateFormatsOnly()
Remove all registered custom date formats => use only the defaults date formats to parse string as date.

Beware that the default formats are expressed in the current local timezone.

Defaults date format are:

Example of valid string date representations:


useDateFormat

@Deprecated
public static void useDateFormat(DateFormat userCustomDateFormat)
Deprecated. : use registerCustomDateFormat(java.text.DateFormat) instead.


useDateFormat

@Deprecated
public static void useDateFormat(String userCustomDateFormatPattern)
Deprecated. : use registerCustomDateFormat(String) instead.


useIsoDateFormat

@Deprecated
public static void useIsoDateFormat()
Deprecated. use useDefaultDateFormatsOnly() instead, it includes this format.


useIsoDateTimeFormat

@Deprecated
public static void useIsoDateTimeFormat()
Deprecated. use useDefaultDateFormatsOnly() instead, it includes this format.


useIsoDateTimeWithMsFormat

@Deprecated
public static void useIsoDateTimeWithMsFormat()
Deprecated. use useDefaultDateFormatsOnly() instead, it includes this format.


useDefaultDateFormats

@Deprecated
public static void useDefaultDateFormats()
Deprecated. use useDefaultDateFormatsOnly() instead.



Copyright © 2013–2015 AssertJ. All rights reserved.