Class Assertions
- java.lang.Object
-
- org.fest.assertions.api.Assertions
-
public class Assertions extends java.lang.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
-
-
Constructor Summary
Constructors Modifier Constructor Description protected
Assertions()
Creates a newAssertions
.
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static <T> Condition<T>
allOf(java.lang.Iterable<? extends Condition<? super T>> conditions)
Creates a newAllOf
static <T> Condition<T>
allOf(Condition<? super T>... conditions)
Creates a newAllOf
static <T> Condition<T>
anyOf(java.lang.Iterable<? extends Condition<? super T>> conditions)
Creates a newAnyOf
static <T> Condition<T>
anyOf(Condition<? super T>... conditions)
Only delegate toAnyOf.anyOf(Condition...)
so that Assertions offers a full feature entry point to all Fest Assert features (but you can useAnyOf
if you prefer).static BooleanAssert
assertThat(boolean actual)
Creates a new instance of
.BooleanAssert
static BooleanArrayAssert
assertThat(boolean[] actual)
Creates a new instance of
.BooleanArrayAssert
static ByteAssert
assertThat(byte actual)
Creates a new instance of
.ByteAssert
static ByteArrayAssert
assertThat(byte[] actual)
Creates a new instance of
.ByteArrayAssert
static CharacterAssert
assertThat(char actual)
Creates a new instance of
.CharacterAssert
static CharArrayAssert
assertThat(char[] actual)
Creates a new instance of
.CharArrayAssert
static DoubleAssert
assertThat(double actual)
Creates a new instance of
.DoubleAssert
static DoubleArrayAssert
assertThat(double[] actual)
Creates a new instance of
.DoubleArrayAssert
static FloatAssert
assertThat(float actual)
Creates a new instance of
.FloatAssert
static FloatArrayAssert
assertThat(float[] actual)
Creates a new instance of
.FloatArrayAssert
static IntegerAssert
assertThat(int actual)
Creates a new instance of
.IntegerAssert
static IntArrayAssert
assertThat(int[] actual)
Creates a new instance of
.IntArrayAssert
static LongAssert
assertThat(long actual)
Creates a new instance of
.LongAssert
static LongArrayAssert
assertThat(long[] actual)
Creates a new instance of
.LongArrayAssert
static ShortAssert
assertThat(short actual)
Creates a new instance of
.ShortAssert
static ShortArrayAssert
assertThat(short[] actual)
Creates a new instance of
.ShortArrayAssert
static FileAssert
assertThat(java.io.File actual)
Creates a new instance of
.FileAssert
static InputStreamAssert
assertThat(java.io.InputStream actual)
Creates a new instance of
.InputStreamAssert
static BooleanAssert
assertThat(java.lang.Boolean actual)
Creates a new instance of
.BooleanAssert
static ByteAssert
assertThat(java.lang.Byte actual)
Creates a new instance of
.ByteAssert
static CharacterAssert
assertThat(java.lang.Character actual)
Creates a new instance of
.CharacterAssert
static DoubleAssert
assertThat(java.lang.Double actual)
Creates a new instance of
.DoubleAssert
static FloatAssert
assertThat(java.lang.Float actual)
Creates a new instance of
.FloatAssert
static IntegerAssert
assertThat(java.lang.Integer actual)
Creates a new instance of
.IntegerAssert
static <T> IterableAssert<T>
assertThat(java.lang.Iterable<T> actual)
Creates a new instance of
.IterableAssert
static LongAssert
assertThat(java.lang.Long actual)
Creates a new instance of
.LongAssert
static ShortAssert
assertThat(java.lang.Short actual)
Creates a new instance of
.ShortAssert
static StringAssert
assertThat(java.lang.String actual)
Creates a new instance of
.StringAssert
static ThrowableAssert
assertThat(java.lang.Throwable actual)
Creates a new instance of
.ThrowableAssert
static BigDecimalAssert
assertThat(java.math.BigDecimal actual)
Creates a new instance of
.BigDecimalAssert
static DateAssert
assertThat(java.util.Date actual)
Creates a new instance of
.DateAssert
static <T> ListAssert<T>
assertThat(java.util.List<T> actual)
Creates a new instance of
.ListAssert
static <K,V>
MapAssert<K,V>assertThat(java.util.Map<K,V> actual)
Creates a new instance of
.MapAssert
static <T> ObjectAssert<T>
assertThat(T actual)
Creates a new instance of
.ObjectAssert
static <T> ObjectArrayAssert<T>
assertThat(T[] actual)
Creates a new instance of
.ObjectArrayAssert
static Index
atIndex(int index)
Only delegate toIndex.atIndex(int)
so that Assertions offers a full feature entry point to all Fest Assert features (but you can useIndex
if you prefer).static java.lang.String
contentOf(java.io.File file)
Loads the text content of a file with the default character set, so that it can be passed toassertThat(String)
.static java.lang.String
contentOf(java.io.File file, java.lang.String charsetName)
Loads the text content of a file, so that it can be passed toassertThat(String)
.static java.lang.String
contentOf(java.io.File file, java.nio.charset.Charset charset)
Loads the text content of a file, so that it can be passed toassertThat(String)
.static <T> DoesNotHave<T>
doesNotHave(Condition<? super T> condition)
Creates a newDoesNotHave
.static MapEntry
entry(java.lang.Object key, java.lang.Object value)
Only delegate toMapEntry.entry(Object, Object)
so that Assertions offers a full feature entry point to all Fest Assert features (but you can useMapEntry
if you prefer).static Properties<java.lang.Object>
extractProperty(java.lang.String propertyName)
Only delegate toProperties.extractProperty(String)
so that Assertions offers a full feature entry point to all Fest Assert features (but you can useProperties
if you prefer).static <T> Properties<T>
extractProperty(java.lang.String propertyName, java.lang.Class<T> propertyType)
Only delegate toProperties.extractProperty(String)
so that Assertions offers a full feature entry point to all Fest Assert features (but you can useProperties
if you prefer).static void
fail(java.lang.String failureMessage)
Only delegate toFail.fail(String)
so that Assertions offers a full feature entry point to all Fest Assert features (but you can use Fail if you prefer).static void
fail(java.lang.String failureMessage, java.lang.Throwable realCause)
Only delegate toFail.fail(String, Throwable)
so that Assertions offers a full feature entry point to all Fest Assert features (but you can use Fail if you prefer).static void
failBecauseExceptionWasNotThrown(java.lang.Class<? extends java.lang.Exception> exceptionClass)
Only delegate toFail.failBecauseExceptionWasNotThrown(Class)
so that Assertions offers a full feature entry point to all Fest Assert features (but you can use Fail if you prefer).static <E> Filters<E>
filter(E[] array)
Only delegate toFilters.filter(Object[])
so that Assertions offers a full feature entry point to all Fest Assert features (but you can useFilters
if you prefer).static <E> Filters<E>
filter(java.lang.Iterable<E> iterableToFilter)
Only delegate toFilters.filter(Object[])
so that Assertions offers a full feature entry point to all Fest Assert features (but you can useFilters
if you prefer).static <T> Not<T>
not(Condition<? super T> condition)
Creates a newNot
.static Offset<java.lang.Double>
offset(java.lang.Double value)
Only delegate toOffset.offset(Double)
so that Assertions offers a full feature entry point to all Fest Assert features (but you can useOffset
if you prefer).static Offset<java.lang.Float>
offset(java.lang.Float value)
Only delegate toOffset.offset(Float)
so that Assertions offers a full feature entry point to all Fest Assert features (but you can useOffset
if you prefer).static void
setRemoveFestRelatedElementsFromStackTrace(boolean removeFestRelatedElementsFromStackTrace)
Only delegate toFail.setRemoveFestRelatedElementsFromStackTrace(boolean)
so that Assertions offers a full feature entry point to all Fest Assert features (but you can useFail
if you prefer).
-
-
-
Constructor Detail
-
Assertions
protected Assertions()
Creates a newAssertions
.
-
-
Method Detail
-
assertThat
public static BigDecimalAssert assertThat(java.math.BigDecimal actual)
Creates a new instance of
.BigDecimalAssert
- Parameters:
actual
- the actual value.- Returns:
- the created assertion object.
-
assertThat
public static BooleanAssert assertThat(boolean actual)
Creates a new instance of
.BooleanAssert
- Parameters:
actual
- the actual value.- Returns:
- the created assertion object.
-
assertThat
public static BooleanAssert assertThat(java.lang.Boolean actual)
Creates a new instance of
.BooleanAssert
- Parameters:
actual
- the actual value.- Returns:
- the created assertion object.
-
assertThat
public static BooleanArrayAssert assertThat(boolean[] actual)
Creates a new instance of
.BooleanArrayAssert
- Parameters:
actual
- the actual value.- Returns:
- the created assertion object.
-
assertThat
public static ByteAssert assertThat(byte actual)
Creates a new instance of
.ByteAssert
- Parameters:
actual
- the actual value.- Returns:
- the created assertion object.
-
assertThat
public static ByteAssert assertThat(java.lang.Byte actual)
Creates a new instance of
.ByteAssert
- Parameters:
actual
- the actual value.- Returns:
- the created assertion object.
-
assertThat
public static ByteArrayAssert assertThat(byte[] actual)
Creates a new instance of
.ByteArrayAssert
- Parameters:
actual
- the actual value.- Returns:
- the created assertion object.
-
assertThat
public static CharacterAssert assertThat(char actual)
Creates a new instance of
.CharacterAssert
- Parameters:
actual
- the actual value.- Returns:
- the created assertion object.
-
assertThat
public static CharArrayAssert assertThat(char[] actual)
Creates a new instance of
.CharArrayAssert
- Parameters:
actual
- the actual value.- Returns:
- the created assertion object.
-
assertThat
public static CharacterAssert assertThat(java.lang.Character actual)
Creates a new instance of
.CharacterAssert
- Parameters:
actual
- the actual value.- Returns:
- the created assertion object.
-
assertThat
public static <T> IterableAssert<T> assertThat(java.lang.Iterable<T> actual)
Creates a new instance of
.IterableAssert
- Parameters:
actual
- the actual value.- Returns:
- the created assertion object.
-
assertThat
public static DoubleAssert assertThat(double actual)
Creates a new instance of
.DoubleAssert
- Parameters:
actual
- the actual value.- Returns:
- the created assertion object.
-
assertThat
public static DoubleAssert assertThat(java.lang.Double actual)
Creates a new instance of
.DoubleAssert
- Parameters:
actual
- the actual value.- Returns:
- the created assertion object.
-
assertThat
public static DoubleArrayAssert assertThat(double[] actual)
Creates a new instance of
.DoubleArrayAssert
- Parameters:
actual
- the actual value.- Returns:
- the created assertion object.
-
assertThat
public static FileAssert assertThat(java.io.File actual)
Creates a new instance of
.FileAssert
- Parameters:
actual
- the actual value.- Returns:
- the created assertion object.
-
assertThat
public static InputStreamAssert assertThat(java.io.InputStream actual)
Creates a new instance of
.InputStreamAssert
- Parameters:
actual
- the actual value.- Returns:
- the created assertion object.
-
assertThat
public static FloatAssert assertThat(float actual)
Creates a new instance of
.FloatAssert
- Parameters:
actual
- the actual value.- Returns:
- the created assertion object.
-
assertThat
public static FloatAssert assertThat(java.lang.Float actual)
Creates a new instance of
.FloatAssert
- Parameters:
actual
- the actual value.- Returns:
- the created assertion object.
-
assertThat
public static FloatArrayAssert assertThat(float[] actual)
Creates a new instance of
.FloatArrayAssert
- Parameters:
actual
- the actual value.- Returns:
- the created assertion object.
-
assertThat
public static IntegerAssert assertThat(int actual)
Creates a new instance of
.IntegerAssert
- Parameters:
actual
- the actual value.- Returns:
- the created assertion object.
-
assertThat
public static IntArrayAssert assertThat(int[] actual)
Creates a new instance of
.IntArrayAssert
- Parameters:
actual
- the actual value.- Returns:
- the created assertion object.
-
assertThat
public static IntegerAssert assertThat(java.lang.Integer actual)
Creates a new instance of
.IntegerAssert
- Parameters:
actual
- the actual value.- Returns:
- the created assertion object.
-
assertThat
public static <T> ListAssert<T> assertThat(java.util.List<T> actual)
Creates a new instance of
.ListAssert
- Parameters:
actual
- the actual value.- Returns:
- the created assertion object.
-
assertThat
public static LongAssert assertThat(long actual)
Creates a new instance of
.LongAssert
- Parameters:
actual
- the actual value.- Returns:
- the created assertion object.
-
assertThat
public static LongAssert assertThat(java.lang.Long actual)
Creates a new instance of
.LongAssert
- Parameters:
actual
- the actual value.- Returns:
- the created assertion object.
-
assertThat
public static LongArrayAssert assertThat(long[] actual)
Creates a new instance of
.LongArrayAssert
- Parameters:
actual
- the actual value.- Returns:
- the created assertion object.
-
assertThat
public static <T> ObjectAssert<T> assertThat(T actual)
Creates a new instance of
.ObjectAssert
- Parameters:
actual
- the actual value.- Returns:
- the created assertion object.
-
assertThat
public static <T> ObjectArrayAssert<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> MapAssert<K,V> assertThat(java.util.Map<K,V> actual)
Creates a new instance of
.MapAssert
- Parameters:
actual
- the actual value.- Returns:
- the created assertion object.
-
assertThat
public static ShortAssert assertThat(short actual)
Creates a new instance of
.ShortAssert
- Parameters:
actual
- the actual value.- Returns:
- the created assertion object.
-
assertThat
public static ShortAssert assertThat(java.lang.Short actual)
Creates a new instance of
.ShortAssert
- Parameters:
actual
- the actual value.- Returns:
- the created assertion object.
-
assertThat
public static ShortArrayAssert assertThat(short[] actual)
Creates a new instance of
.ShortArrayAssert
- Parameters:
actual
- the actual value.- Returns:
- the created assertion object.
-
assertThat
public static StringAssert assertThat(java.lang.String actual)
Creates a new instance of
.StringAssert
- Parameters:
actual
- the actual value.- Returns:
- the created assertion object.
-
assertThat
public static DateAssert assertThat(java.util.Date actual)
Creates a new instance of
.DateAssert
- Parameters:
actual
- the actual value.- Returns:
- the created assertion object.
-
assertThat
public static ThrowableAssert assertThat(java.lang.Throwable actual)
Creates a new instance of
.ThrowableAssert
- Parameters:
actual
- the actual value.- Returns:
- the created assertion Throwable.
-
setRemoveFestRelatedElementsFromStackTrace
public static void setRemoveFestRelatedElementsFromStackTrace(boolean removeFestRelatedElementsFromStackTrace)
Only delegate toFail.setRemoveFestRelatedElementsFromStackTrace(boolean)
so that Assertions offers a full feature entry point to all Fest Assert features (but you can useFail
if you prefer).
-
fail
public static void fail(java.lang.String failureMessage)
Only delegate toFail.fail(String)
so that Assertions offers a full feature entry point to all Fest Assert features (but you can use Fail if you prefer).
-
fail
public static void fail(java.lang.String failureMessage, java.lang.Throwable realCause)
Only delegate toFail.fail(String, Throwable)
so that Assertions offers a full feature entry point to all Fest Assert features (but you can use Fail if you prefer).
-
failBecauseExceptionWasNotThrown
public static void failBecauseExceptionWasNotThrown(java.lang.Class<? extends java.lang.Exception> exceptionClass)
Only delegate toFail.failBecauseExceptionWasNotThrown(Class)
so that Assertions offers a full feature entry point to all Fest Assert features (but you can use Fail if you prefer).
-
extractProperty
public static <T> Properties<T> extractProperty(java.lang.String propertyName, java.lang.Class<T> propertyType)
Only delegate toProperties.extractProperty(String)
so that Assertions offers a full feature entry point to all Fest Assert features (but you can useProperties
if you prefer).Typical usage is to chain
extractProperty
withfrom
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<java.lang.Object> extractProperty(java.lang.String propertyName)
Only delegate toProperties.extractProperty(String)
so that Assertions offers a full feature entry point to all Fest Assert features (but you can useProperties
if you prefer).Typical usage is to chain
extractProperty
withfrom
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");
-
entry
public static MapEntry entry(java.lang.Object key, java.lang.Object value)
Only delegate toMapEntry.entry(Object, Object)
so that Assertions offers a full feature entry point to all Fest Assert features (but you can useMapEntry
if you prefer).Typical usage is to call
entry
in MapAssertcontains
assertion, see examples below :assertThat(ringBearers).contains(entry(oneRing, frodo), entry(nenya, galadriel));
-
atIndex
public static Index atIndex(int index)
Only delegate toIndex.atIndex(int)
so that Assertions offers a full feature entry point to all Fest Assert features (but you can useIndex
if you prefer).Typical usage :
List
elvesRings = newArrayList(vilya, nenya, narya); assertThat(elvesRings).contains(vilya, atIndex(0)).contains(nenya, atIndex(1)).contains(narya, atIndex(2));
-
offset
public static Offset<java.lang.Double> offset(java.lang.Double value)
Only delegate toOffset.offset(Double)
so that Assertions offers a full feature entry point to all Fest Assert features (but you can useOffset
if you prefer).Typical usage :
assertThat(8.1).isEqualTo(8.0, offset(0.1));
-
offset
public static Offset<java.lang.Float> offset(java.lang.Float value)
Only delegate toOffset.offset(Float)
so that Assertions offers a full feature entry point to all Fest Assert features (but you can useOffset
if you prefer).Typical usage :
assertThat(8.2f).isEqualTo(8.0f, offset(0.2f));
-
allOf
public static <T> Condition<T> allOf(Condition<? super T>... conditions)
Creates a newAllOf
- Type Parameters:
T
- the type of object the given condition accept.- Parameters:
conditions
- the conditions to evaluate.- Returns:
- the created
AnyOf
. - Throws:
java.lang.NullPointerException
- if the given array isnull
.java.lang.NullPointerException
- if any of the elements in the given array isnull
.
-
allOf
public static <T> Condition<T> allOf(java.lang.Iterable<? extends Condition<? super T>> conditions)
Creates a newAllOf
- Type Parameters:
T
- the type of object the given condition accept.- Parameters:
conditions
- the conditions to evaluate.- Returns:
- the created
AnyOf
. - Throws:
java.lang.NullPointerException
- if the given iterable isnull
.java.lang.NullPointerException
- if any of the elements in the given iterable isnull
.
-
anyOf
public static <T> Condition<T> anyOf(Condition<? super T>... conditions)
Only delegate toAnyOf.anyOf(Condition...)
so that Assertions offers a full feature entry point to all Fest Assert features (but you can useAnyOf
if you prefer).Typical usage (
jedi
andsith
areCondition
) :assertThat("Vader").is(anyOf(jedi, sith));
-
anyOf
public static <T> Condition<T> anyOf(java.lang.Iterable<? extends Condition<? super T>> conditions)
Creates a newAnyOf
- Type Parameters:
T
- the type of object the given condition accept.- Parameters:
conditions
- the conditions to evaluate.- Returns:
- the created
AnyOf
. - Throws:
java.lang.NullPointerException
- if the given iterable isnull
.java.lang.NullPointerException
- if any of the elements in the given iterable isnull
.
-
doesNotHave
public static <T> DoesNotHave<T> doesNotHave(Condition<? super T> condition)
Creates a newDoesNotHave
.- Parameters:
condition
- the condition to inverse.- Returns:
- The Not condition created.
-
not
public static <T> Not<T> not(Condition<? super T> condition)
Creates a newNot
.- Parameters:
condition
- the condition to inverse.- Returns:
- The Not condition created.
-
filter
public static <E> Filters<E> filter(E[] array)
Only delegate toFilters.filter(Object[])
so that Assertions offers a full feature entry point to all Fest Assert features (but you can useFilters
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(java.lang.Iterable<E> iterableToFilter)
Only delegate toFilters.filter(Object[])
so that Assertions offers a full feature entry point to all Fest Assert features (but you can useFilters
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 java.lang.String contentOf(java.io.File file, java.nio.charset.Charset charset)
Loads the text content of a file, so that it can be passed toassertThat(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:
java.lang.NullPointerException
- if the given charset isnull
.org.fest.util.FilesException
- if an I/O exception occurs.
-
contentOf
public static java.lang.String contentOf(java.io.File file, java.lang.String charsetName)
Loads the text content of a file, so that it can be passed toassertThat(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:
java.lang.IllegalArgumentException
- if the given character set is not supported on this platform.org.fest.util.FilesException
- if an I/O exception occurs.
-
contentOf
public static java.lang.String contentOf(java.io.File file)
Loads the text content of a file with the default character set, so that it can be passed toassertThat(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:
org.fest.util.FilesException
- if an I/O exception occurs.
-
-