org.fest.assertions
Class CollectionAssert

java.lang.Object
  extended by org.fest.assertions.Assert
      extended by org.fest.assertions.GenericAssert<T>
          extended by org.fest.assertions.GroupAssert<Collection<?>>
              extended by org.fest.assertions.CollectionAssert

public class CollectionAssert
extends GroupAssert<Collection<?>>

Understands assertions for collections. To create a new instance of this class use the method Assertions.assertThat(Collection).

Author:
Yvonne Wang, Alex Ruiz

Field Summary
 
Fields inherited from class org.fest.assertions.GenericAssert
actual
 
Constructor Summary
protected CollectionAssert(Collection<?> actual)
          Creates a new CollectionAssert.
 
Method Summary
protected  int actualGroupSize()
          Returns the number of elements in the actual collection.
 CollectionAssert as(Description description)
          Sets the description of the actual value, to be used in as message of any AssertionError thrown when an assertion fails.
 CollectionAssert as(String description)
          Sets the description of the actual value, to be used in as message of any AssertionError thrown when an assertion fails.
 CollectionAssert contains(Object... objects)
          Verifies that the actual collection contains the given objects.
 CollectionAssert containsOnly(Object... objects)
          Verifies that the actual collection contains the given objects only.
 CollectionAssert describedAs(Description description)
          Alias for GenericAssert.as(Description), since "as" is a keyword in Groovy.
 CollectionAssert describedAs(String description)
          Alias for GenericAssert.as(String), since "as" is a keyword in Groovy.
 CollectionAssert doesNotHaveDuplicates()
          Verifies that the actual collection does not have duplicates.
 CollectionAssert doesNotSatisfy(Condition<Collection<?>> condition)
          Verifies that the actual collection does not satisfy the given condition.
 CollectionAssert excludes(Object... objects)
          Verifies that the actual collection does not contain the given objects.
 CollectionAssert hasSize(int expected)
          Verifies that the number of elements in the actual collection is equal to the given one.
 CollectionAssert is(Condition<Collection<?>> condition)
          Alias for satisfies(Condition).
 void isEmpty()
          Verifies that the actual collection is empty (not null with zero elements.)
 CollectionAssert isEqualTo(Collection<?> expected)
          Verifies that the actual collection is equal to the given one.
 CollectionAssert isNot(Condition<Collection<?>> condition)
          Alias for doesNotSatisfy(Condition).
 CollectionAssert isNotEmpty()
          Verifies that the actual collection contains at least on element.
 CollectionAssert isNotEqualTo(Collection<?> other)
          Verifies that the actual collection is not equal to the given one.
 CollectionAssert isNotNull()
          Verifies that the actual collection is not null.
 CollectionAssert isNotSameAs(Collection<?> other)
          Verifies that the actual collection is not the same as the given one.
 void isNullOrEmpty()
          Verifies that the actual collection is null or empty.
 CollectionAssert isSameAs(Collection<?> expected)
          Verifies that the actual collection is the same as the given one.
 CollectionAssert overridingErrorMessage(String message)
          Replaces the default message displayed in case of a failure with the given one.
 CollectionAssert satisfies(Condition<Collection<?>> condition)
          Verifies that the actual collection satisfies the given condition.
 
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
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

CollectionAssert

protected CollectionAssert(Collection<?> actual)
Creates a new CollectionAssert.

Parameters:
actual - the target to verify.
Method Detail

contains

public CollectionAssert contains(Object... objects)
Verifies that the actual collection contains the given objects.

Parameters:
objects - the objects to look for.
Returns:
this assertion object.
Throws:
AssertionError - if the actual collection is null.
NullPointerException - if the given array is null.
AssertionError - if the actual collection does not contain the given objects.

containsOnly

public CollectionAssert containsOnly(Object... objects)
Verifies that the actual collection contains the given objects only.

Parameters:
objects - the objects to look for.
Returns:
this assertion object.
Throws:
AssertionError - if the actual collection is null.
NullPointerException - if the given array is null.
AssertionError - if the actual collection does not contain the given objects, or if the actual collection contains elements other than the ones specified.

excludes

public CollectionAssert excludes(Object... objects)
Verifies that the actual collection does not contain the given objects.

Parameters:
objects - the objects that the collection should exclude.
Returns:
this assertion object.
Throws:
AssertionError - if the actual collection is null.
NullPointerException - if the given array is null.
AssertionError - if the actual collection contains any of the given objects.

doesNotHaveDuplicates

public CollectionAssert doesNotHaveDuplicates()
Verifies that the actual collection does not have duplicates.

Returns:
this assertion object.
Throws:
AssertionError - if the actual collection is null.
AssertionError - if the actual collection has duplicates.

as

public CollectionAssert as(String description)
Sets the description of the actual value, to be used in as message of any AssertionError thrown when an assertion fails. This method should be called before any assertion method, otherwise any assertion failure will not show the provided description.

For example:

 assertThat(val).as("name").isEqualTo("Frodo");
 

Specified by:
as in class GenericAssert<Collection<?>>
Parameters:
description - the description of the actual value.
Returns:
this assertion object.

describedAs

public CollectionAssert describedAs(String description)
Alias for GenericAssert.as(String), 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.

For example:

 assertThat(val).describedAs("name").isEqualTo("Frodo");
 

Specified by:
describedAs in class GenericAssert<Collection<?>>
Parameters:
description - the description of the actual value.
Returns:
this assertion object.

as

public CollectionAssert as(Description description)
Sets the description of the actual value, to be used in as message of any AssertionError thrown when an assertion fails. This method should be called before any assertion method, otherwise any assertion failure will not show the provided description.

For example:

 assertThat(val).as(new BasicDescription("name")).isEqualTo("Frodo");
 

Specified by:
as in class GenericAssert<Collection<?>>
Parameters:
description - the description of the actual value.
Returns:
this assertion object.

describedAs

public CollectionAssert describedAs(Description description)
Alias for GenericAssert.as(Description), 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.

For example:

 assertThat(val).describedAs(new BasicDescription("name")).isEqualTo("Frodo");
 

Specified by:
describedAs in class GenericAssert<Collection<?>>
Parameters:
description - the description of the actual value.
Returns:
this assertion object.

satisfies

public CollectionAssert satisfies(Condition<Collection<?>> condition)
Verifies that the actual collection satisfies the given condition.

Specified by:
satisfies in class GenericAssert<Collection<?>>
Parameters:
condition - the given condition.
Returns:
this assertion object.
Throws:
NullPointerException - if the given condition is null.
AssertionError - if the actual collection does not satisfy the given condition.
See Also:
is(Condition)

doesNotSatisfy

public CollectionAssert doesNotSatisfy(Condition<Collection<?>> condition)
Verifies that the actual collection does not satisfy the given condition.

Specified by:
doesNotSatisfy in class GenericAssert<Collection<?>>
Parameters:
condition - the given condition.
Returns:
this assertion object.
Throws:
NullPointerException - if the given condition is null.
AssertionError - if the actual collection satisfies the given condition.
See Also:
isNot(Condition)

is

public CollectionAssert is(Condition<Collection<?>> condition)
Alias for satisfies(Condition).

Specified by:
is in class GenericAssert<Collection<?>>
Parameters:
condition - the given condition.
Returns:
this assertion object.
Throws:
NullPointerException - if the given condition is null.
AssertionError - if the actual collection does not satisfy the given condition.
Since:
1.2

isNot

public CollectionAssert isNot(Condition<Collection<?>> condition)
Alias for doesNotSatisfy(Condition).

Specified by:
isNot in class GenericAssert<Collection<?>>
Parameters:
condition - the given condition.
Returns:
this assertion object.
Throws:
NullPointerException - if the given condition is null.
AssertionError - if the actual collection satisfies the given condition.
Since:
1.2

isNullOrEmpty

public void isNullOrEmpty()
Verifies that the actual collection is null or empty.

Specified by:
isNullOrEmpty in class GroupAssert<Collection<?>>
Throws:
AssertionError - if the actual collection is not null or not empty.

isNotNull

public CollectionAssert isNotNull()
Verifies that the actual collection is not null.

Specified by:
isNotNull in class GenericAssert<Collection<?>>
Returns:
this assertion object.
Throws:
AssertionError - if the actual collection is null.

isEmpty

public void isEmpty()
Verifies that the actual collection is empty (not null with zero elements.)

Specified by:
isEmpty in class GroupAssert<Collection<?>>
Throws:
AssertionError - if the actual collection is null.
AssertionError - if the actual collection is not empty.

isNotEmpty

public CollectionAssert isNotEmpty()
Verifies that the actual collection contains at least on element.

Specified by:
isNotEmpty in class GroupAssert<Collection<?>>
Returns:
this assertion object.
Throws:
AssertionError - if the actual collection is null.
AssertionError - if the actual collection is empty.

hasSize

public CollectionAssert hasSize(int expected)
Verifies that the number of elements in the actual collection is equal to the given one.

Specified by:
hasSize in class GroupAssert<Collection<?>>
Parameters:
expected - the expected number of elements in the actual collection.
Returns:
this assertion object.
Throws:
AssertionError - if the actual collection is null.
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 class GroupAssert<Collection<?>>
Returns:
the number of elements in the actual collection.

isEqualTo

public CollectionAssert isEqualTo(Collection<?> expected)
Verifies that the actual collection is equal to the given one.

Specified by:
isEqualTo in class GenericAssert<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

public CollectionAssert isNotEqualTo(Collection<?> other)
Verifies that the actual collection is not equal to the given one.

Specified by:
isNotEqualTo in class GenericAssert<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

public CollectionAssert isSameAs(Collection<?> expected)
Verifies that the actual collection is the same as the given one.

Specified by:
isSameAs in class GenericAssert<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

public CollectionAssert isNotSameAs(Collection<?> other)
Verifies that the actual collection is not the same as the given one.

Specified by:
isNotSameAs in class GenericAssert<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

public CollectionAssert overridingErrorMessage(String message)
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 class GenericAssert<Collection<?>>
Parameters:
message - the given error message, which will replace the default one.
Returns:
this assertion.


Copyright © 2007-2011 FEST (Fixtures for Easy Software Testing). All Rights Reserved.