Package org.fest.assertions
Class ListAssert
java.lang.Object
Understands assertions for
List
s. To create a new instance of this class use the method
Assertions.assertThat(List)
.- Since:
- 1.1
-
Field Summary
Fields inherited from class org.fest.assertions.GenericAssert
actual
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionReturns the actual value as aList
.Returns the actual value as aSet
.protected int
Returns the number of elements in the actual
.List
Sets the description of the actual value, to be used in as message of any
thrown when an assertion fails.AssertionError
as
(Description description) Sets the description of the actual value, to be used in as message of any
thrown when an assertion fails.AssertionError
Verifies that the actual
contains the given objects, in any order.List
Verifies that the actual
contains the given object at the given index.List
containsExactly
(Object... objects) Verifies that the actual
contains the given objects, in the same order.List
containsOnly
(Object... objects) Verifies that the actual
contains the given objects only, in any order.List
containsSequence
(Object... sequence) Verifies that the actual
contains the given sequence of objects, without any other objects between them.List
describedAs
(String description) Alias for
, since "as" is a keyword in Groovy.GenericAssert.as(String)
describedAs
(Description description) Alias for
, since "as" is a keyword in Groovy.GenericAssert.as(Description)
Verifies that the actual
does not have duplicates.List
doesNotSatisfy
(Condition<List<?>> condition) Verifies that the actual
does not satisfy the given condition.List
Verifies that the actual
ends with the given sequence of objects, without any other objects between them.List
Verifies that the actual
does not contain the given objects.List
private void
failElementNotFound
(Object e, Object a, int index) private void
failIfNotEndingWithSequence
(Object[] notFound) private void
failIfNotStartingWithSequence
(Object[] notFound) private void
failIfSequenceNotFound
(Object[] notFound) private void
failIndexOutOfBounds
(int index) hasSize
(int expected) Verifies that the number of elements in the actual
is equal to the given one.List
Alias for
.satisfies(Condition)
Verifies that the actual
is equal to the given one.List
Alias for
.doesNotSatisfy(Condition)
Verifies that the actual
contains at least on element.List
isNotEqualTo
(List<?> other) Verifies that the actual
is not equal to the given one.List
Verifies that the actual
is notList
null
.isNotSameAs
(List<?> other) Verifies that the actual
is not the same as the given one.List
Verifies that the actual
is the same as the given one.List
onProperty
(String propertyName) Creates a new instance of
whose target list contains the values of the given property name from the elements of thisListAssert
ListAssert
's list.overridingErrorMessage
(String message) Replaces the default message displayed in case of a failure with the given one.Verifies that the actual
satisfies the given condition.List
startsWith
(Object... sequence) Verifies that the actual
starts with the given sequence of objects, without any other objects between them.List
Methods inherited from class org.fest.assertions.ItemGroupAssert
assertContains, assertContainsOnly, assertDoesNotHaveDuplicates, assertExcludes, asSet, validateIsNotNull
Methods inherited from class org.fest.assertions.GroupAssert
assertHasSize, assertIsNotEmpty, isEmpty, isNullOrEmpty
Methods inherited from class org.fest.assertions.GenericAssert
assertDoesNotSatisfy, assertEqualTo, assertIs, assertIsNot, assertNotEqualTo, assertNotNull, assertNotSameAs, assertSameAs, assertSatisfies, isNull
Methods inherited from class org.fest.assertions.Assert
customErrorMessage, description, description, description, equals, fail, fail, failIfCustomMessageIsSet, failIfCustomMessageIsSet, failure, formattedErrorMessage, hashCode, rawDescription, replaceDefaultErrorMessagesWith
-
Constructor Details
-
ListAssert
Creates a newListAssert
.- Parameters:
actual
- the target to verify.
-
-
Method Details
-
contains
Verifies that the actual
contains the given object at the given index.List
- Parameters:
o
- the object to look for.index
- the index where the object should be stored in the actualList
.- Returns:
- this assertion object.
- Throws:
NullPointerException
- if the givenIndex
isnull
.IndexOutOfBoundsException
- if the value of the givenIndex
is negative, or equal to or greater than the size of the actualList
.AssertionError
- if the givenList
does not contain the given object at the given index.
-
failElementNotFound
-
failIndexOutOfBounds
private void failIndexOutOfBounds(int index) -
containsSequence
Verifies that the actual
contains the given sequence of objects, without any other objects between them.List
- Parameters:
sequence
- the sequence of objects to look for.- Returns:
- this assertion object.
- Throws:
AssertionError
- if the actualList
isnull
.AssertionError
- if the given array isnull
.AssertionError
- if the actualList
does not contain the given sequence of objects.
-
failIfSequenceNotFound
-
startsWith
Verifies that the actual
starts with the given sequence of objects, without any other objects between them. Same asList
, but verifies also that first given object is also first element ofcontainsSequence(java.lang.Object...)
List
.- Parameters:
sequence
- the sequence of objects to look for.- Returns:
- this assertion object.
- Throws:
AssertionError
- if the actualList
isnull
.AssertionError
- if the given array isnull
.AssertionError
- if the actualList
is not empty and with the given sequence of objects is empty.AssertionError
- if the actualList
does not start with the given sequence of objects.
-
failIfNotStartingWithSequence
-
endsWith
Verifies that the actual
ends with the given sequence of objects, without any other objects between them. Same asList
, but verifies also that last given object is also last element ofcontainsSequence(java.lang.Object...)
List
.- Parameters:
sequence
- the sequence of objects to look for.- Returns:
- this assertion object.
- Throws:
AssertionError
- if the actualList
isnull
.AssertionError
- if the given array isnull
.AssertionError
- if the actualList
is not empty and with the given sequence of objects is empty.AssertionError
- if the actualList
does not end with the given sequence of objects.
-
failIfNotEndingWithSequence
-
contains
Verifies that the actual
contains the given objects, in any order.List
- Specified by:
contains
in classObjectGroupAssert<List<?>>
- Parameters:
objects
- the objects to look for.- Returns:
- this assertion object.
- Throws:
AssertionError
- if the actualList
isnull
.NullPointerException
- if the given array isnull
.AssertionError
- if the actualList
does not contain the given objects.
-
containsOnly
Verifies that the actual
contains the given objects only, in any order.List
- Specified by:
containsOnly
in classObjectGroupAssert<List<?>>
- Parameters:
objects
- the objects to look for.- Returns:
- this assertion object.
- Throws:
AssertionError
- if the actualList
isnull
.NullPointerException
- if the given array isnull
.AssertionError
- if the actualList
does not contain the given objects, or if the actualList
contains elements other than the ones specified.
-
excludes
Verifies that the actual
does not contain the given objects.List
- Specified by:
excludes
in classObjectGroupAssert<List<?>>
- Parameters:
objects
- the objects that theList
should exclude.- Returns:
- this assertion object.
- Throws:
AssertionError
- if the actualList
isnull
.NullPointerException
- if the given array isnull
.AssertionError
- if the actualList
contains any of the given objects.
-
doesNotHaveDuplicates
Verifies that the actual
does not have duplicates.List
- Specified by:
doesNotHaveDuplicates
in classObjectGroupAssert<List<?>>
- Returns:
- this assertion object.
- Throws:
AssertionError
- if the actualList
isnull
.AssertionError
- if the actualList
has duplicates.
-
as
Sets the description of the actual value, to be used in as message of any
thrown when an assertion fails. This method should be called before any assertion method, otherwise any assertion failure will not show the provided description.AssertionError
For example:
assertThat(val).as("name").isEqualTo("Frodo");
- Specified by:
as
in classObjectGroupAssert<List<?>>
- Parameters:
description
- the description of the actual value.- Returns:
- this assertion object.
-
describedAs
Alias for
, since "as" is a keyword in Groovy. This method should be called before any assertion method, otherwise any assertion failure will not show the provided description.GenericAssert.as(String)
For example:
assertThat(val).describedAs("name").isEqualTo("Frodo");
- Specified by:
describedAs
in classObjectGroupAssert<List<?>>
- Parameters:
description
- the description of the actual value.- Returns:
- this assertion object.
-
as
Sets the description of the actual value, to be used in as message of any
thrown when an assertion fails. This method should be called before any assertion method, otherwise any assertion failure will not show the provided description.AssertionError
For example:
assertThat(val).as(new BasicDescription("name")).isEqualTo("Frodo");
- Specified by:
as
in classObjectGroupAssert<List<?>>
- Parameters:
description
- the description of the actual value.- Returns:
- this assertion object.
-
describedAs
Alias for
, since "as" is a keyword in Groovy. This method should be called before any assertion method, otherwise any assertion failure will not show the provided description.GenericAssert.as(Description)
For example:
assertThat(val).describedAs(new BasicDescription("name")).isEqualTo("Frodo");
- Specified by:
describedAs
in classObjectGroupAssert<List<?>>
- Parameters:
description
- the description of the actual value.- Returns:
- this assertion object.
-
satisfies
Verifies that the actual
satisfies the given condition.List
- Specified by:
satisfies
in classGenericAssert<List<?>>
- Parameters:
condition
- the given condition.- Returns:
- this assertion object.
- Throws:
NullPointerException
- if the given condition isnull
.AssertionError
- if the actualList
does not satisfy the given condition.- See Also:
-
doesNotSatisfy
Verifies that the actual
does not satisfy the given condition.List
- Specified by:
doesNotSatisfy
in classGenericAssert<List<?>>
- Parameters:
condition
- the given condition.- Returns:
- this assertion object.
- Throws:
NullPointerException
- if the given condition isnull
.AssertionError
- if the actualList
satisfies the given condition.- See Also:
-
is
Alias for
.satisfies(Condition)
- Specified by:
is
in classGenericAssert<List<?>>
- Parameters:
condition
- the given condition.- Returns:
- this assertion object.
- Throws:
NullPointerException
- if the given condition isnull
.AssertionError
- if the actualList
does not satisfy the given condition.- Since:
- 1.2
-
isNot
Alias for
.doesNotSatisfy(Condition)
- Specified by:
isNot
in classGenericAssert<List<?>>
- Parameters:
condition
- the given condition.- Returns:
- this assertion object.
- Throws:
NullPointerException
- if the given condition isnull
.AssertionError
- if the actualList
satisfies the given condition.- Since:
- 1.2
-
hasSize
Verifies that the number of elements in the actual
is equal to the given one.List
- Specified by:
hasSize
in classGroupAssert<List<?>>
- Parameters:
expected
- the expected number of elements in the actualList
.- Returns:
- this assertion object.
- Throws:
AssertionError
- if the actualList
isnull
.AssertionError
- if the number of elements of the actualList
is not equal to the given one.
-
isNotEmpty
Verifies that the actual
contains at least on element.List
- Specified by:
isNotEmpty
in classGroupAssert<List<?>>
- Returns:
- this assertion object.
- Throws:
AssertionError
- if the actualList
isnull
.AssertionError
- if the actualList
is empty.
-
actualGroupSize
protected int actualGroupSize()Returns the number of elements in the actual
.List
- Specified by:
actualGroupSize
in classGroupAssert<List<?>>
- Returns:
- the number of elements in the actual
List
. - Throws:
AssertionError
- if the actualList
isnull
.
-
isNotNull
Verifies that the actual
is notList
null
.- Specified by:
isNotNull
in classGenericAssert<List<?>>
- Returns:
- this assertion object.
- Throws:
AssertionError
- if the actualList
isnull
.
-
containsExactly
Verifies that the actual
contains the given objects, in the same order. This method works just likeList
, with the difference that internally the given array is converted to aisEqualTo(List)
List
.- Parameters:
objects
- the objects to look for.- Returns:
- this assertion object.
- Throws:
AssertionError
- if the actualList
isnull
.NullPointerException
- if the given array isnull
.AssertionError
- if the actualList
does not contain the given objects.
-
isEqualTo
Verifies that the actual
is equal to the given one.List
- Specified by:
isEqualTo
in classGenericAssert<List<?>>
- Parameters:
expected
- the givenList
to compare the actualList
to.- Returns:
- this assertion object.
- Throws:
AssertionError
- if the actualList
is not equal to the given one.
-
isNotEqualTo
Verifies that the actual
is not equal to the given one.List
- Specified by:
isNotEqualTo
in classGenericAssert<List<?>>
- Parameters:
other
- the givenList
to compare the actualList
to.- Returns:
- this assertion object.
- Throws:
AssertionError
- if the actualList
is equal to the given one.
-
isSameAs
Verifies that the actual
is the same as the given one.List
- Specified by:
isSameAs
in classGenericAssert<List<?>>
- Parameters:
expected
- the givenList
to compare the actualList
to.- Returns:
- this assertion object.
- Throws:
AssertionError
- if the actualList
is not the same as the given one.
-
isNotSameAs
Verifies that the actual
is not the same as the given one.List
- Specified by:
isNotSameAs
in classGenericAssert<List<?>>
- Parameters:
other
- the givenList
to compare the actualList
to.- Returns:
- this assertion object.
- Throws:
AssertionError
- if the actualList
is the same as the given one.
-
overridingErrorMessage
Replaces the default message displayed in case of a failure with the given one.For example, the following assertion:
assertThat("Hello").isEqualTo("Bye");
will fail with the default message "expected:<'[Bye]'> but was:<'[Hello]'>."We can replace this message with our own:
assertThat("Hello").overridingErrorMessage("'Hello' should be equal to 'Bye'").isEqualTo("Bye");
in this case, the assertion will fail showing the message "'Hello' should be equal to 'Bye'".- Specified by:
overridingErrorMessage
in classObjectGroupAssert<List<?>>
- Parameters:
message
- the given error message, which will replace the default one.- Returns:
- this assertion.
-
onProperty
Creates a new instance of
whose target list contains the values of the given property name from the elements of thisListAssert
ListAssert
's list. Property access works with both simple properties likePerson.age
and nested propertiesPerson.father.age
.For example, let's say we have a list of
Person
objects and you want to verify their age:assertThat(persons).onProperty("age").containsOnly(25, 16, 44, 37); // simple property assertThat(persons).onProperty("father.age").containsOnly(55, 46, 74, 62); // nested property
- Specified by:
onProperty
in classObjectGroupAssert<List<?>>
- Parameters:
propertyName
- the name of the property to extract values from the actual list to build a newListAssert
.- Returns:
- a new
ListAssert
containing the values of the given property name from the elements of thisListAssert
's list. - Throws:
AssertionError
- if the actual list isnull
.org.fest.util.IntrospectionError
- if an element in the given list does not have a matching property.- Since:
- 1.3
-
actualAsSet
Returns the actual value as aSet
.- Specified by:
actualAsSet
in classItemGroupAssert<List<?>>
- Returns:
- the actual value as a
Set
.
-
actualAsList
Returns the actual value as aList
.- Specified by:
actualAsList
in classItemGroupAssert<List<?>>
- Returns:
- the actual value as a
List
.
-