Top Description Methods
org.assertj.core.api

public Interface EnumerableAssert<SELF extends EnumerableAssert<SELF, ELEMENT>, ELEMENT>

Known Direct Subinterfaces
org.assertj.core.api.ObjectEnumerableAssert
Known Direct Implementers
org.assertj.core.api.AbstractCharSequenceAssert, org.assertj.core.api.AbstractEnumerableAssert, org.assertj.core.api.AbstractMapAssert
Type Parameters
<SELF>
the "self" type of this assertion class. Please read "Emulating 'self types' using Java Generics to simplify fluent API implementation" for more details.
<ELEMENT>
the type of elements of the "actual" value.
Imports
java.util.Comparator

Assertions applicable to groups of values that can be enumerated (e.g. arrays, collections or strings.)
Authors
Yvonne Wang, Alex Ruiz, Mikhail Mazursky, Nicolas François

Method Summary

Modifier and TypeMethod and Description
public SELF

Returns:

this assertion object.
hasSameSizeAs
(Iterable<?>
the Iterable to compare size with actual group.
other
)

Verifies that the actual group has the same size as given Iterable.

public SELF

Returns:

this assertion object.
hasSameSizeAs
(Object
the array to compare size with actual group.
array
)

Verifies that the actual group has the same size as given array.

public SELF

Returns:

this assertion object.
hasSize
(int
the expected number of values in the actual group.
expected
)

Verifies that the number of values in the actual group is equal to the given one.

public SELF

Returns:

this assertion object.
hasSizeBetween
(int
the lower boundary compared to which actual size should be greater than or equal to.
lowerBoundary
,
int
the higher boundary compared to which actual size should be less than or equal to.
higherBoundary
)

Verifies that the number of values in the actual group is between the given boundaries (inclusive).

public SELF

Returns:

this assertion object.
hasSizeGreaterThan
(int
the given value to compare the actual size to.
boundary
)

Verifies that the number of values in the actual group is greater than the given boundary.

public SELF

Returns:

this assertion object.
hasSizeGreaterThanOrEqualTo
(int
the given value to compare the actual size to.
boundary
)

Verifies that the number of values in the actual group is greater than or equal to the given boundary.

public SELF

Returns:

this assertion object.
hasSizeLessThan
(int
the given value to compare the actual size to.
boundary
)

Verifies that the number of values in the actual group is less than the given boundary.

public SELF

Returns:

this assertion object.
hasSizeLessThanOrEqualTo
(int
the given value to compare the actual size to.
boundary
)

Verifies that the number of values in the actual group is less than or equal to the given boundary.

public void
isEmpty()

Verifies that the actual group of values is empty.

public SELF

Returns:

this assertion object.
isNotEmpty
()

Verifies that the actual group of values is not empty.

public void
isNullOrEmpty()

Verifies that the actual group of values is null or empty.

public SELF

Returns:

this assertion object.
usingDefaultElementComparator
()

Revert to standard comparison for incoming assertion group element checks.

public SELF

Returns:

this assertion object.
usingElementComparator
(Comparator<? super ELEMENT>
the comparator to use for incoming assertion checks.
customComparator
)

Use given custom comparator instead of relying on actual type A equals method to compare group elements for incoming assertion checks.

Method Detail

hasSameSizeAsback to summary
public SELF hasSameSizeAs(Iterable<?> other)

Verifies that the actual group has the same size as given Iterable.

Example:

 Iterable<String> abc = newArrayList("a", "b", "c");
Iterable<Ring> elvesRings = newArrayList(vilya, nenya, narya);

// assertion will pass
assertThat(elvesRings).hasSameSizeAs(abc);

// assertions will fail
assertThat(elvesRings).hasSameSizeAs(Arrays.asList(1, 2));
assertThat(elvesRings).hasSameSizeAs(Arrays.asList(1, 2, 3, 4));
Parameters
other:Iterable<?>

the Iterable to compare size with actual group.

Returns:SELF

this assertion object.

Exceptions
AssertionError:
  • if the actual group is null.
  • if the other Iterable is null.
  • if actual group and given Iterable don't have the same size.
hasSameSizeAsback to summary
public SELF hasSameSizeAs(Object array)

Verifies that the actual group has the same size as given array.

Parameter is declared as Object to accept both Object[] and primitive arrays (e.g. int[]).

Example:
 int[] oneTwoThree = {1, 2, 3};
Iterable<Ring> elvesRings = newArrayList(vilya, nenya, narya);

// assertion will pass
assertThat(elvesRings).hasSameSizeAs(oneTwoThree);

// assertions will fail
assertThat(elvesRings).hasSameSizeAs(new int[] { 1, 2});
assertThat(elvesRings).hasSameSizeAs(new int[] { 1, 2, 3, 4});
Parameters
array:Object

the array to compare size with actual group.

Returns:SELF

this assertion object.

Exceptions
AssertionError:
  • if the actual group is null.
  • if the array parameter is null or is not a true array.
  • if actual group and given array don't have the same size.
hasSizeback to summary
public SELF hasSize(int expected)

Verifies that the number of values in the actual group is equal to the given one.

Example:

 // assertions will pass
assertThat(new String[] { "a", "b" }).hasSize(2);
assertThat(Arrays.asList(1, 2, 3)).hasSize(3);

// assertions will fail
assertThat(new ArrayList()).hasSize(1);
assertThat(new int[] { 1, 2, 3 }).hasSize(2);
Parameters
expected:int

the expected number of values in the actual group.

Returns:SELF

this assertion object.

Exceptions
AssertionError:
if the number of values of the actual group is not equal to the given one.
hasSizeBetweenback to summary
public SELF hasSizeBetween(int lowerBoundary, int higherBoundary)

Verifies that the number of values in the actual group is between the given boundaries (inclusive).

Example:

 // assertions will pass
assertThat(new String[] { "a", "b" }).hasSizeBetween(0, 4);
assertThat(Arrays.asList(1, 2, 3)).hasSizeBetween(2, 3)
                                  .hasSizeBetween(3, 4)
                                  .hasSizeBetween(3, 3);

// assertions will fail
assertThat(new ArrayList()).hasSizeBetween(1, 3);
assertThat(new int[] { 1, 2, 3 }).hasSizeBetween(4, 6);
assertThat(new int[] { 1, 2, 3, 4 }).hasSizeBetween(0, 2);
Parameters
lowerBoundary:int

the lower boundary compared to which actual size should be greater than or equal to.

higherBoundary:int

the higher boundary compared to which actual size should be less than or equal to.

Returns:SELF

this assertion object.

Exceptions
AssertionError:
if the number of values of the actual group is not between the boundaries.
hasSizeGreaterThanback to summary
public SELF hasSizeGreaterThan(int boundary)

Verifies that the number of values in the actual group is greater than the given boundary.

Example:

 // assertions will pass
assertThat(new String[] { "a", "b" }).hasSizeGreaterThan(1);
assertThat(Arrays.asList(1, 2, 3)).hasSizeGreaterThan(2);

// assertions will fail
assertThat(Arrays.asList(1, 2, 3)).hasSizeGreaterThan(3);
assertThat(new int[] { 1, 2, 3 }).hasSizeGreaterThan(6);
Parameters
boundary:int

the given value to compare the actual size to.

Returns:SELF

this assertion object.

Exceptions
AssertionError:
if the number of values of the actual group is not greater than the boundary.
hasSizeGreaterThanOrEqualToback to summary
public SELF hasSizeGreaterThanOrEqualTo(int boundary)

Verifies that the number of values in the actual group is greater than or equal to the given boundary.

Example:

 // assertions will pass
assertThat(Arrays.asList(1, 2, 3)).hasSizeGreaterThanOrEqualTo(3);
assertThat(Arrays.asList(1, 2)).hasSizeGreaterThanOrEqualTo(1);

// assertions will fail
assertThat(Arrays.asList(1, 2)).hasSizeGreaterThanOrEqualTo(3);
assertThat(new int[] { 1, 2, 3 }).hasSizeGreaterThanOrEqualTo(4);
Parameters
boundary:int

the given value to compare the actual size to.

Returns:SELF

this assertion object.

Exceptions
AssertionError:
if the number of values of the actual group is not greater than or equal to the boundary.
hasSizeLessThanback to summary
public SELF hasSizeLessThan(int boundary)

Verifies that the number of values in the actual group is less than the given boundary.

Example:

 // assertions will pass
assertThat(new String[] { "a", "b" }).hasSizeLessThan(5);
assertThat(Arrays.asList(1, 2, 3)).hasSizeLessThan(4);

// assertions will fail
assertThat(Arrays.asList(1, 2, 3)).hasSizeLessThan(3);
assertThat(new int[] { 1, 2, 3 }).hasSizeLessThan(2);
Parameters
boundary:int

the given value to compare the actual size to.

Returns:SELF

this assertion object.

Exceptions
AssertionError:
if the number of values of the actual group is not less than the boundary.
hasSizeLessThanOrEqualToback to summary
public SELF hasSizeLessThanOrEqualTo(int boundary)

Verifies that the number of values in the actual group is less than or equal to the given boundary.

Example:

 // assertions will pass
assertThat(new String[] { "a", "b" }).hasSizeLessThanOrEqualTo(3);
assertThat(Arrays.asList(1, 2, 3)).hasSizeLessThanOrEqualTo(5)
                                  .hasSizeLessThanOrEqualTo(3);

// assertions will fail
assertThat(Arrays.asList(1, 2, 3)).hasSizeLessThanOrEqualTo(2);
assertThat(new int[] { 1, 2, 3 }).hasSizeLessThanOrEqualTo(1);
Parameters
boundary:int

the given value to compare the actual size to.

Returns:SELF

this assertion object.

Exceptions
AssertionError:
if the number of values of the actual group is not less than or equal to the boundary.
isEmptyback to summary
public void isEmpty()

Verifies that the actual group of values is empty.

Example:

 // assertions will pass
assertThat(new ArrayList()).isEmpty();
assertThat(new int[] { }).isEmpty();

// assertions will fail
assertThat(new String[] { "a", "b" }).isEmpty();
assertThat(Arrays.asList(1, 2, 3)).isEmpty();
Exceptions
AssertionError:
if the actual group of values is not empty.
isNotEmptyback to summary
public SELF isNotEmpty()

Verifies that the actual group of values is not empty.

Example:

 // assertions will pass
assertThat(new String[] { "a", "b" }).isNotEmpty();
assertThat(Arrays.asList(1, 2, 3)).isNotEmpty();

// assertions will fail
assertThat(new ArrayList()).isNotEmpty();
assertThat(new int[] { }).isNotEmpty();
Returns:SELF

this assertion object.

Exceptions
AssertionError:
if the actual group of values is empty.
isNullOrEmptyback to summary
public void isNullOrEmpty()

Verifies that the actual group of values is null or empty.

Example:

 // assertions will pass
List<String> strings = new ArrayList<>();
assertThat(strings).isNullOrEmpty();
assertThat(new int[] { }).isNullOrEmpty();

// assertions will fail
assertThat(new String[] { "a", "b"}).isNullOrEmpty();
assertThat(Arrays.asList(1, 2, 3)).isNullOrEmpty();
Exceptions
AssertionError:
if the actual group of values is not null or not empty.
usingDefaultElementComparatorback to summary
public SELF usingDefaultElementComparator()

Revert to standard comparison for incoming assertion group element checks.

This method should be used to disable a custom comparison strategy set by calling usingElementComparator(Comparator).

Returns:SELF

this assertion object.

usingElementComparatorback to summary
public SELF usingElementComparator(Comparator<? super ELEMENT> customComparator)

Use given custom comparator instead of relying on actual type A equals method to compare group elements for incoming assertion checks.

Custom comparator is bound to assertion instance, meaning that if a new assertion is created, it will use default comparison strategy.

Examples :
 // compares invoices by payee
assertThat(invoiceList).usingComparator(invoicePayeeComparator).isEqualTo(expectedInvoiceList);

// compares invoices by date, doesNotHaveDuplicates and contains both use the given invoice date comparator
assertThat(invoiceList).usingComparator(invoiceDateComparator).doesNotHaveDuplicates().contains(may2010Invoice);

// as assertThat(invoiceList) creates a new assertion, it falls back to standard comparison strategy
// based on Invoice's equal method to compare invoiceList elements to lowestInvoice.
assertThat(invoiceList).contains(lowestInvoice);

// standard comparison : the fellowshipOfTheRing includes Gandalf but not Sauron (believe me) ...
assertThat(fellowshipOfTheRing).contains(gandalf)
                               .doesNotContain(sauron);

// ... but if we compare only races, Sauron is in fellowshipOfTheRing because he's a Maia like Gandalf.
assertThat(fellowshipOfTheRing).usingElementComparator(raceComparator)
                               .contains(sauron);
Parameters
customComparator:Comparator<? super ELEMENT>

the comparator to use for incoming assertion checks.

Returns:SELF

this assertion object.

Exceptions
NullPointerException:
if the given comparator is null.