Package org.fest.assertions
Class CollectionAssert
java.lang.Object
Understands assertions for collections. To create a new instance of this class use the method
Assertions.assertThat(Collection)
.-
Field Summary
Fields inherited from class org.fest.assertions.GenericAssert
actual
-
Constructor Summary
ConstructorsModifierConstructorDescriptionprotected
CollectionAssert
(Collection<?> actual) Creates a newCollectionAssert
. -
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 collection.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 collection contains the given objects.containsOnly
(Object... objects) Verifies that the actual collection contains the given objects only.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 collection does not have duplicates.doesNotSatisfy
(Condition<Collection<?>> condition) Verifies that the actual collection does not satisfy the given condition.Verifies that the actual collection does not contain the given objects.hasSize
(int expected) Verifies that the number of elements in the actual collection is equal to the given one.is
(Condition<Collection<?>> condition) Alias for
.satisfies(Condition)
isEqualTo
(Collection<?> expected) Verifies that the actual collection is equal to the given one.isNot
(Condition<Collection<?>> condition) Alias for
.doesNotSatisfy(Condition)
Verifies that the actual collection contains at least on element.isNotEqualTo
(Collection<?> other) Verifies that the actual collection is not equal to the given one.Verifies that the actual collection is notnull
.isNotSameAs
(Collection<?> other) Verifies that the actual collection is not the same as the given one.isSameAs
(Collection<?> expected) Verifies that the actual collection is the same as the given one.onProperty
(String propertyName) Creates a new instance of
whose target collection contains the values of the given property name from the elements of thisCollectionAssert
CollectionAssert
's collection.overridingErrorMessage
(String message) Replaces the default message displayed in case of a failure with the given one.satisfies
(Condition<Collection<?>> condition) Verifies that the actual collection satisfies the given condition.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
-
CollectionAssert
Creates a newCollectionAssert
.- Parameters:
actual
- the target to verify.
-
-
Method Details
-
contains
Verifies that the actual collection contains the given objects.- Specified by:
contains
in classObjectGroupAssert<Collection<?>>
- Parameters:
objects
- the objects to look for.- Returns:
- this assertion object.
- Throws:
AssertionError
- if the actual collection isnull
.NullPointerException
- if the given array isnull
.AssertionError
- if the actual collection does not contain the given objects.
-
containsOnly
Verifies that the actual collection contains the given objects only.- Specified by:
containsOnly
in classObjectGroupAssert<Collection<?>>
- Parameters:
objects
- the objects to look for.- Returns:
- this assertion object.
- Throws:
AssertionError
- if the actual collection isnull
.NullPointerException
- if the given array isnull
.AssertionError
- if the actual collection does not contain the given objects, or if the actual collection contains elements other than the ones specified.
-
excludes
Verifies that the actual collection does not contain the given objects.- Specified by:
excludes
in classObjectGroupAssert<Collection<?>>
- Parameters:
objects
- the objects that the collection should exclude.- Returns:
- this assertion object.
- Throws:
AssertionError
- if the actual collection isnull
.NullPointerException
- if the given array isnull
.AssertionError
- if the actual collection contains any of the given objects.
-
doesNotHaveDuplicates
Verifies that the actual collection does not have duplicates.- Specified by:
doesNotHaveDuplicates
in classObjectGroupAssert<Collection<?>>
- Returns:
- this assertion object.
- Throws:
AssertionError
- if the actual collection isnull
.AssertionError
- if the actual collection 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<Collection<?>>
- 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<Collection<?>>
- 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<Collection<?>>
- 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<Collection<?>>
- Parameters:
description
- the description of the actual value.- Returns:
- this assertion object.
-
satisfies
Verifies that the actual collection satisfies the given condition.- Specified by:
satisfies
in classGenericAssert<Collection<?>>
- Parameters:
condition
- the given condition.- Returns:
- this assertion object.
- Throws:
NullPointerException
- if the given condition isnull
.AssertionError
- if the actual collection does not satisfy the given condition.- See Also:
-
doesNotSatisfy
Verifies that the actual collection does not satisfy the given condition.- Specified by:
doesNotSatisfy
in classGenericAssert<Collection<?>>
- Parameters:
condition
- the given condition.- Returns:
- this assertion object.
- Throws:
NullPointerException
- if the given condition isnull
.AssertionError
- if the actual collection satisfies the given condition.- See Also:
-
is
Alias for
.satisfies(Condition)
- Specified by:
is
in classGenericAssert<Collection<?>>
- Parameters:
condition
- the given condition.- Returns:
- this assertion object.
- Throws:
NullPointerException
- if the given condition isnull
.AssertionError
- if the actual collection does not satisfy the given condition.- Since:
- 1.2
-
isNot
Alias for
.doesNotSatisfy(Condition)
- Specified by:
isNot
in classGenericAssert<Collection<?>>
- Parameters:
condition
- the given condition.- Returns:
- this assertion object.
- Throws:
NullPointerException
- if the given condition isnull
.AssertionError
- if the actual collection satisfies the given condition.- Since:
- 1.2
-
isNotNull
Verifies that the actual collection is notnull
.- Specified by:
isNotNull
in classGenericAssert<Collection<?>>
- Returns:
- this assertion object.
- Throws:
AssertionError
- if the actual collection isnull
.
-
isNotEmpty
Verifies that the actual collection contains at least on element.- Specified by:
isNotEmpty
in classGroupAssert<Collection<?>>
- Returns:
- this assertion object.
- Throws:
AssertionError
- if the actual collection isnull
.AssertionError
- if the actual collection is empty.
-
hasSize
Verifies that the number of elements in the actual collection is equal to the given one.- Specified by:
hasSize
in classGroupAssert<Collection<?>>
- Parameters:
expected
- the expected number of elements in the actual collection.- Returns:
- this assertion object.
- Throws:
AssertionError
- if the actual collection isnull
.AssertionError
- if the number of elements of the actual collection is not equal to the given one.
-
actualGroupSize
protected int actualGroupSize()Returns the number of elements in the actual collection.- Specified by:
actualGroupSize
in classGroupAssert<Collection<?>>
- Returns:
- the number of elements in the actual collection.
- Throws:
AssertionError
- if the actual collection isnull
.
-
isEqualTo
Verifies that the actual collection is equal to the given one.- Specified by:
isEqualTo
in classGenericAssert<Collection<?>>
- Parameters:
expected
- the given collection to compare the actual collection to.- Returns:
- this assertion object.
- Throws:
AssertionError
- if the actual collection is not equal to the given one.
-
isNotEqualTo
Verifies that the actual collection is not equal to the given one.- Specified by:
isNotEqualTo
in classGenericAssert<Collection<?>>
- Parameters:
other
- the given collection to compare the actual collection to.- Returns:
- this assertion object.
- Throws:
AssertionError
- if the actual collection is equal to the given one.
-
isSameAs
Verifies that the actual collection is the same as the given one.- Specified by:
isSameAs
in classGenericAssert<Collection<?>>
- Parameters:
expected
- the given collection to compare the actual collection to.- Returns:
- this assertion object.
- Throws:
AssertionError
- if the actual collection is not the same as the given one.
-
isNotSameAs
Verifies that the actual collection is not the same as the given one.- Specified by:
isNotSameAs
in classGenericAssert<Collection<?>>
- Parameters:
other
- the given collection to compare the actual collection to.- Returns:
- this assertion object.
- Throws:
AssertionError
- if the actual collection 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<Collection<?>>
- Parameters:
message
- the given error message, which will replace the default one.- Returns:
- this assertion.
-
onProperty
Creates a new instance of
whose target collection contains the values of the given property name from the elements of thisCollectionAssert
CollectionAssert
's collection. Property access works with both simple properties likePerson.age
and nested propertiesPerson.father.age
.For example, let's say we have a collection 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<Collection<?>>
- Parameters:
propertyName
- the name of the property to extract values from the actual collection to build a newCollectionAssert
.- Returns:
- a new
CollectionAssert
containing the values of the given property name from the elements of thisCollectionAssert
's collection. - Throws:
AssertionError
- if the actual collection isnull
.org.fest.util.IntrospectionError
- if an element in the given collection does not have a matching property.- Since:
- 1.3
-
actualAsSet
Returns the actual value as aSet
.- Specified by:
actualAsSet
in classItemGroupAssert<Collection<?>>
- Returns:
- the actual value as a
Set
.
-
actualAsList
Returns the actual value as aList
.- Specified by:
actualAsList
in classItemGroupAssert<Collection<?>>
- Returns:
- the actual value as a
List
.
-