org.fest.assertions
Class ListAssert

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

public class ListAssert
extends GroupAssert<List<?>>

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

Since:
1.1
Author:
Alex Ruiz

Field Summary
 
Fields inherited from class org.fest.assertions.GenericAssert
actual
 
Constructor Summary
protected ListAssert(List<?> actual)
          Creates a new ListAssert.
 
Method Summary
protected  int actualGroupSize()
          Returns the number of elements in the actual List.
 ListAssert as(Description description)
          Sets the description of the actual value, to be used in as message of any AssertionError thrown when an assertion fails.
 ListAssert as(String description)
          Sets the description of the actual value, to be used in as message of any AssertionError thrown when an assertion fails.
 ListAssert contains(Object... objects)
          Verifies that the actual List contains the given objects, in any order.
 ListAssert contains(Object o, Index index)
          Verifies that the actual List contains the given object at the given index.
 ListAssert containsExactly(Object... objects)
          Verifies that the actual List contains the given objects, in the same order.
 ListAssert containsOnly(Object... objects)
          Verifies that the actual List contains the given objects only, in any order.
 ListAssert containsSequence(Object... sequence)
          Verifies that the actual List contains the given sequence of objects, without any other objects between them.
 ListAssert describedAs(Description description)
          Alias for GenericAssert.as(Description), since "as" is a keyword in Groovy.
 ListAssert describedAs(String description)
          Alias for GenericAssert.as(String), since "as" is a keyword in Groovy.
 ListAssert doesNotHaveDuplicates()
          Verifies that the actual List does not have duplicates.
 ListAssert doesNotSatisfy(Condition<List<?>> condition)
          Verifies that the actual List does not satisfy the given condition.
 ListAssert endsWith(Object... sequence)
          Verifies that the actual List ends with the given sequence of objects, without any other objects between them.
 ListAssert excludes(Object... objects)
          Verifies that the actual List does not contain the given objects.
 ListAssert hasSize(int expected)
          Verifies that the number of elements in the actual List is equal to the given one.
 ListAssert is(Condition<List<?>> condition)
          Alias for satisfies(Condition).
 void isEmpty()
          Verifies that the actual List is empty (not null with zero elements.)
 ListAssert isEqualTo(List<?> expected)
          Verifies that the actual List is equal to the given one.
 ListAssert isNot(Condition<List<?>> condition)
          Alias for doesNotSatisfy(Condition).
 ListAssert isNotEmpty()
          Verifies that the actual List contains at least on element.
 ListAssert isNotEqualTo(List<?> other)
          Verifies that the actual List is not equal to the given one.
 ListAssert isNotNull()
          Verifies that the actual List is not null.
 ListAssert isNotSameAs(List<?> other)
          Verifies that the actual List is not the same as the given one.
 void isNullOrEmpty()
          Verifies that the actual List is null or empty.
 ListAssert isSameAs(List<?> expected)
          Verifies that the actual List is the same as the given one.
 ListAssert overridingErrorMessage(String message)
          Replaces the default message displayed in case of a failure with the given one.
 ListAssert satisfies(Condition<List<?>> condition)
          Verifies that the actual List satisfies the given condition.
 ListAssert startsWith(Object... sequence)
          Verifies that the actual List starts with the given sequence of objects, without any other objects between them.
 
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

ListAssert

protected ListAssert(List<?> actual)
Creates a new ListAssert.

Parameters:
actual - the target to verify.
Method Detail

contains

public ListAssert contains(Object o,
                           Index index)
Verifies that the actual List contains the given object at the given index.

Parameters:
o - the object to look for.
index - the index where the object should be stored in the actual List.
Returns:
this assertion object.
Throws:
NullPointerException - if the given Index is null.
IndexOutOfBoundsException - if the value of the given Index is negative, or equal to or greater than the size of the actual List.
AssertionError - if the given List does not contain the given object at the given index.

containsSequence

public ListAssert containsSequence(Object... sequence)
Verifies that the actual List contains the given sequence of objects, without any other objects between them.

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

startsWith

public ListAssert startsWith(Object... sequence)
Verifies that the actual List starts with the given sequence of objects, without any other objects between them. Same as containsSequence(java.lang.Object...), but verifies also that first given object is also first element of List.

Parameters:
sequence - the sequence of objects to look for.
Returns:
this assertion object.
Throws:
AssertionError - if the actual List is null.
AssertionError - if the given array is null.
AssertionError - if the actual List is not empty and with the given sequence of objects is empty.
AssertionError - if the actual List does not start with the given sequence of objects.

endsWith

public ListAssert endsWith(Object... sequence)
Verifies that the actual List ends with the given sequence of objects, without any other objects between them. Same as containsSequence(java.lang.Object...), but verifies also that last given object is also last element of List.

Parameters:
sequence - the sequence of objects to look for.
Returns:
this assertion object.
Throws:
AssertionError - if the actual List is null.
AssertionError - if the given array is null.
AssertionError - if the actual List is not empty and with the given sequence of objects is empty.
AssertionError - if the actual List does not end with the given sequence of objects.

contains

public ListAssert contains(Object... objects)
Verifies that the actual List contains the given objects, in any order.

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

containsOnly

public ListAssert containsOnly(Object... objects)
Verifies that the actual List contains the given objects only, in any order.

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

excludes

public ListAssert excludes(Object... objects)
Verifies that the actual List does not contain the given objects.

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

doesNotHaveDuplicates

public ListAssert doesNotHaveDuplicates()
Verifies that the actual List does not have duplicates.

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

as

public ListAssert 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<List<?>>
Parameters:
description - the description of the actual value.
Returns:
this assertion object.

describedAs

public ListAssert 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<List<?>>
Parameters:
description - the description of the actual value.
Returns:
this assertion object.

as

public ListAssert 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<List<?>>
Parameters:
description - the description of the actual value.
Returns:
this assertion object.

describedAs

public ListAssert 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<List<?>>
Parameters:
description - the description of the actual value.
Returns:
this assertion object.

satisfies

public ListAssert satisfies(Condition<List<?>> condition)
Verifies that the actual List satisfies the given condition.

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

doesNotSatisfy

public ListAssert doesNotSatisfy(Condition<List<?>> condition)
Verifies that the actual List does not satisfy the given condition.

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

is

public ListAssert is(Condition<List<?>> condition)
Alias for satisfies(Condition).

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

isNot

public ListAssert isNot(Condition<List<?>> condition)
Alias for doesNotSatisfy(Condition).

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

hasSize

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

Specified by:
hasSize in class GroupAssert<List<?>>
Parameters:
expected - the expected number of elements in the actual List.
Returns:
this assertion object.
Throws:
AssertionError - if the actual List is null.
AssertionError - if the number of elements of the actual List is not equal to the given one.

actualGroupSize

protected int actualGroupSize()
Returns the number of elements in the actual List.

Specified by:
actualGroupSize in class GroupAssert<List<?>>
Returns:
the number of elements in the actual List.

isEmpty

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

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

isNotEmpty

public ListAssert isNotEmpty()
Verifies that the actual List contains at least on element.

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

isNullOrEmpty

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

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

isNotNull

public ListAssert isNotNull()
Verifies that the actual List is not null.

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

containsExactly

public ListAssert containsExactly(Object... objects)
Verifies that the actual List contains the given objects, in the same order. This method works just like isEqualTo(List), with the difference that internally the given array is converted to a List.

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

isEqualTo

public ListAssert isEqualTo(List<?> expected)
Verifies that the actual List is equal to the given one.

Specified by:
isEqualTo in class GenericAssert<List<?>>
Parameters:
expected - the given List to compare the actual List to.
Returns:
this assertion object.
Throws:
AssertionError - if the actual List is not equal to the given one.

isNotEqualTo

public ListAssert isNotEqualTo(List<?> other)
Verifies that the actual List is not equal to the given one.

Specified by:
isNotEqualTo in class GenericAssert<List<?>>
Parameters:
other - the given List to compare the actual List to.
Returns:
this assertion object.
Throws:
AssertionError - if the actual List is equal to the given one.

isSameAs

public ListAssert isSameAs(List<?> expected)
Verifies that the actual List is the same as the given one.

Specified by:
isSameAs in class GenericAssert<List<?>>
Parameters:
expected - the given List to compare the actual List to.
Returns:
this assertion object.
Throws:
AssertionError - if the actual List is not the same as the given one.

isNotSameAs

public ListAssert isNotSameAs(List<?> other)
Verifies that the actual List is not the same as the given one.

Specified by:
isNotSameAs in class GenericAssert<List<?>>
Parameters:
other - the given List to compare the actual List to.
Returns:
this assertion object.
Throws:
AssertionError - if the actual List is the same as the given one.

overridingErrorMessage

public ListAssert 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<List<?>>
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.