Package org.fest.assertions
Class ListAssert
java.lang.Object
Understands assertions for
Lists. 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 intReturns the number of elements in the actual.ListSets the description of the actual value, to be used in as message of anythrown when an assertion fails.AssertionErroras(Description description) Sets the description of the actual value, to be used in as message of anythrown when an assertion fails.AssertionErrorVerifies that the actualcontains the given objects, in any order.ListVerifies that the actualcontains the given object at the given index.ListcontainsExactly(Object... objects) Verifies that the actualcontains the given objects, in the same order.ListcontainsOnly(Object... objects) Verifies that the actualcontains the given objects only, in any order.ListcontainsSequence(Object... sequence) Verifies that the actualcontains the given sequence of objects, without any other objects between them.ListdescribedAs(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 actualdoes not have duplicates.ListdoesNotSatisfy(Condition<List<?>> condition) Verifies that the actualdoes not satisfy the given condition.ListVerifies that the actualends with the given sequence of objects, without any other objects between them.ListVerifies that the actualdoes not contain the given objects.Listprivate voidfailElementNotFound(Object e, Object a, int index) private voidfailIfNotEndingWithSequence(Object[] notFound) private voidfailIfNotStartingWithSequence(Object[] notFound) private voidfailIfSequenceNotFound(Object[] notFound) private voidfailIndexOutOfBounds(int index) hasSize(int expected) Verifies that the number of elements in the actualis equal to the given one.ListAlias for.satisfies(Condition)Verifies that the actualis equal to the given one.ListAlias for.doesNotSatisfy(Condition)Verifies that the actualcontains at least on element.ListisNotEqualTo(List<?> other) Verifies that the actualis not equal to the given one.ListVerifies that the actualis notListnull.isNotSameAs(List<?> other) Verifies that the actualis not the same as the given one.ListVerifies that the actualis the same as the given one.ListonProperty(String propertyName) Creates a new instance ofwhose target list contains the values of the given property name from the elements of thisListAssertListAssert's list.overridingErrorMessage(String message) Replaces the default message displayed in case of a failure with the given one.Verifies that the actualsatisfies the given condition.ListstartsWith(Object... sequence) Verifies that the actualstarts with the given sequence of objects, without any other objects between them.ListMethods inherited from class org.fest.assertions.ItemGroupAssert
assertContains, assertContainsOnly, assertDoesNotHaveDuplicates, assertExcludes, asSet, validateIsNotNullMethods inherited from class org.fest.assertions.GroupAssert
assertHasSize, assertIsNotEmpty, isEmpty, isNullOrEmptyMethods inherited from class org.fest.assertions.GenericAssert
assertDoesNotSatisfy, assertEqualTo, assertIs, assertIsNot, assertNotEqualTo, assertNotNull, assertNotSameAs, assertSameAs, assertSatisfies, isNullMethods 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 actualcontains 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 givenIndexisnull.IndexOutOfBoundsException- if the value of the givenIndexis negative, or equal to or greater than the size of the actualList.AssertionError- if the givenListdoes not contain the given object at the given index.
-
failElementNotFound
-
failIndexOutOfBounds
private void failIndexOutOfBounds(int index) -
containsSequence
Verifies that the actualcontains 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 actualListisnull.AssertionError- if the given array isnull.AssertionError- if the actualListdoes not contain the given sequence of objects.
-
failIfSequenceNotFound
-
startsWith
Verifies that the actualstarts 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 actualListisnull.AssertionError- if the given array isnull.AssertionError- if the actualListis not empty and with the given sequence of objects is empty.AssertionError- if the actualListdoes not start with the given sequence of objects.
-
failIfNotStartingWithSequence
-
endsWith
Verifies that the actualends 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 actualListisnull.AssertionError- if the given array isnull.AssertionError- if the actualListis not empty and with the given sequence of objects is empty.AssertionError- if the actualListdoes not end with the given sequence of objects.
-
failIfNotEndingWithSequence
-
contains
Verifies that the actualcontains the given objects, in any order.List- Specified by:
containsin classObjectGroupAssert<List<?>>- Parameters:
objects- the objects to look for.- Returns:
- this assertion object.
- Throws:
AssertionError- if the actualListisnull.NullPointerException- if the given array isnull.AssertionError- if the actualListdoes not contain the given objects.
-
containsOnly
Verifies that the actualcontains the given objects only, in any order.List- Specified by:
containsOnlyin classObjectGroupAssert<List<?>>- Parameters:
objects- the objects to look for.- Returns:
- this assertion object.
- Throws:
AssertionError- if the actualListisnull.NullPointerException- if the given array isnull.AssertionError- if the actualListdoes not contain the given objects, or if the actualListcontains elements other than the ones specified.
-
excludes
Verifies that the actualdoes not contain the given objects.List- Specified by:
excludesin classObjectGroupAssert<List<?>>- Parameters:
objects- the objects that theListshould exclude.- Returns:
- this assertion object.
- Throws:
AssertionError- if the actualListisnull.NullPointerException- if the given array isnull.AssertionError- if the actualListcontains any of the given objects.
-
doesNotHaveDuplicates
Verifies that the actualdoes not have duplicates.List- Specified by:
doesNotHaveDuplicatesin classObjectGroupAssert<List<?>>- Returns:
- this assertion object.
- Throws:
AssertionError- if the actualListisnull.AssertionError- if the actualListhas duplicates.
-
as
Sets the description of the actual value, to be used in as message of anythrown when an assertion fails. This method should be called before any assertion method, otherwise any assertion failure will not show the provided description.AssertionErrorFor example:
assertThat(val).as("name").isEqualTo("Frodo");- Specified by:
asin 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:
describedAsin 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 anythrown when an assertion fails. This method should be called before any assertion method, otherwise any assertion failure will not show the provided description.AssertionErrorFor example:
assertThat(val).as(new BasicDescription("name")).isEqualTo("Frodo");- Specified by:
asin 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:
describedAsin classObjectGroupAssert<List<?>>- Parameters:
description- the description of the actual value.- Returns:
- this assertion object.
-
satisfies
Verifies that the actualsatisfies the given condition.List- Specified by:
satisfiesin classGenericAssert<List<?>>- Parameters:
condition- the given condition.- Returns:
- this assertion object.
- Throws:
NullPointerException- if the given condition isnull.AssertionError- if the actualListdoes not satisfy the given condition.- See Also:
-
doesNotSatisfy
Verifies that the actualdoes not satisfy the given condition.List- Specified by:
doesNotSatisfyin classGenericAssert<List<?>>- Parameters:
condition- the given condition.- Returns:
- this assertion object.
- Throws:
NullPointerException- if the given condition isnull.AssertionError- if the actualListsatisfies the given condition.- See Also:
-
is
Alias for.satisfies(Condition)- Specified by:
isin classGenericAssert<List<?>>- Parameters:
condition- the given condition.- Returns:
- this assertion object.
- Throws:
NullPointerException- if the given condition isnull.AssertionError- if the actualListdoes not satisfy the given condition.- Since:
- 1.2
-
isNot
Alias for.doesNotSatisfy(Condition)- Specified by:
isNotin classGenericAssert<List<?>>- Parameters:
condition- the given condition.- Returns:
- this assertion object.
- Throws:
NullPointerException- if the given condition isnull.AssertionError- if the actualListsatisfies the given condition.- Since:
- 1.2
-
hasSize
Verifies that the number of elements in the actualis equal to the given one.List- Specified by:
hasSizein classGroupAssert<List<?>>- Parameters:
expected- the expected number of elements in the actualList.- Returns:
- this assertion object.
- Throws:
AssertionError- if the actualListisnull.AssertionError- if the number of elements of the actualListis not equal to the given one.
-
isNotEmpty
Verifies that the actualcontains at least on element.List- Specified by:
isNotEmptyin classGroupAssert<List<?>>- Returns:
- this assertion object.
- Throws:
AssertionError- if the actualListisnull.AssertionError- if the actualListis empty.
-
actualGroupSize
protected int actualGroupSize()Returns the number of elements in the actual.List- Specified by:
actualGroupSizein classGroupAssert<List<?>>- Returns:
- the number of elements in the actual
List. - Throws:
AssertionError- if the actualListisnull.
-
isNotNull
Verifies that the actualis notListnull.- Specified by:
isNotNullin classGenericAssert<List<?>>- Returns:
- this assertion object.
- Throws:
AssertionError- if the actualListisnull.
-
containsExactly
Verifies that the actualcontains 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 actualListisnull.NullPointerException- if the given array isnull.AssertionError- if the actualListdoes not contain the given objects.
-
isEqualTo
Verifies that the actualis equal to the given one.List- Specified by:
isEqualToin classGenericAssert<List<?>>- Parameters:
expected- the givenListto compare the actualListto.- Returns:
- this assertion object.
- Throws:
AssertionError- if the actualListis not equal to the given one.
-
isNotEqualTo
Verifies that the actualis not equal to the given one.List- Specified by:
isNotEqualToin classGenericAssert<List<?>>- Parameters:
other- the givenListto compare the actualListto.- Returns:
- this assertion object.
- Throws:
AssertionError- if the actualListis equal to the given one.
-
isSameAs
Verifies that the actualis the same as the given one.List- Specified by:
isSameAsin classGenericAssert<List<?>>- Parameters:
expected- the givenListto compare the actualListto.- Returns:
- this assertion object.
- Throws:
AssertionError- if the actualListis not the same as the given one.
-
isNotSameAs
Verifies that the actualis not the same as the given one.List- Specified by:
isNotSameAsin classGenericAssert<List<?>>- Parameters:
other- the givenListto compare the actualListto.- Returns:
- this assertion object.
- Throws:
AssertionError- if the actualListis 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:
overridingErrorMessagein classObjectGroupAssert<List<?>>- Parameters:
message- the given error message, which will replace the default one.- Returns:
- this assertion.
-
onProperty
Creates a new instance ofwhose target list contains the values of the given property name from the elements of thisListAssertListAssert's list. Property access works with both simple properties likePerson.ageand nested propertiesPerson.father.age.For example, let's say we have a list of
Personobjects 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:
onPropertyin classObjectGroupAssert<List<?>>- Parameters:
propertyName- the name of the property to extract values from the actual list to build a newListAssert.- Returns:
- a new
ListAssertcontaining 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:
actualAsSetin classItemGroupAssert<List<?>>- Returns:
- the actual value as a
Set.
-
actualAsList
Returns the actual value as aList.- Specified by:
actualAsListin classItemGroupAssert<List<?>>- Returns:
- the actual value as a
List.
-