Common assertions for Java collections in AssertJ
Tags: java, tutorial, webdev, testing
Published: 2020-04-03 17:19:27

---
title: Common assertions for Java collections in AssertJ
published: true
description: 
tags: java, tutorial, webdev, testing
canonical_url: https://mednikov.net/common-assertj-assertions-for-java-collections/
cover_image: https://cdn.pixabay.com/photo/2015/01/14/18/41/home-office-599475_1280.jpg
---
Hello! As part of Java collection series, I decided to write this small post to list some of most common assertion methods from AssertJ, that can be used to test Java collections.

## Common collection assertions

This section presents most common [assertions](https://mednikov.net/common-assertj-assertions-for-java-collections/), used with Java collections. Examples are illustrated using [array-based lists](https://mednikov.net/array-based-lists-in-java/), that are most used Java data structures.

### Collection contains the element

Basic situation is to check if the collection contains or does not contain the specific element. AssertJ validates this using _contains()_/_doesNotContain()_ element which have overloaded versions:

* _contains(E e, int index)_ = checks that the element presents on the specific position
* _contains(E... elements)_ = accepts varargs, e.g. one or more elements and checks for their presence in __any__ order

Take a look on the code snippet below:

```java
@Test
public void containsElementTest(){
    List<Integer> numbers = Lists.newArrayList(1, 52, 12, 39, 45, 98, 100, 565, 6, 13);
    assertThat(numbers).contains(12);
    assertThat(numbers).doesNotContain(50);
}
```

### Collection contains elements in any order

In order to verify, that collection contains a group of elements, it is possible to use two scenarios:

* Use _contains(E.. elements)_ method and supply elements as varargs
* Use _contains(Iterable i)_ method that accepts another collection and validates that all elements are contained inside the original one in __any__ order

Here is the example of using _containsAll()_:

```java
@Test
public void containsAllElementsNoMatterOrderTest(){
    List<Integer> numbers = Lists.newArrayList(1, 52, 12, 39, 45, 98, 100, 565, 6, 13);
    List<Integer> values = Lists.newArrayList(52, 39, 12, 1, 100);
    assertThat(numbers).containsAll(values);
}
```

### Collection contains elements in the specific order

Another case is to validate, that the collection contains not only all specified elements, but also keep the __specific order__. To do this, it is possible to use _containsExactlyElementsOf()_ method. It accepts an iterable and verifies that actual collection contains all the elements of the given iterable and nothing else in the __same order__.

Take a look on following example:

```java
@Test
public void containsAllElementsInOrderTest(){
    List<Integer> numbers = Lists.newArrayList(1, 52, 12, 39, 45, 98, 100, 565, 6, 13);
    List<Integer> values = Lists.newArrayList(numbers);
    assertThat(numbers).containsExactlyElementsOf(values);
}
```

### Collection contains no duplicates

Not all [Java collections](https://mednikov.net/java-util-collection/) allows duplicates: sets, for example, check for inserted elements and do not allow duplicates. Other collections, like lists, do not offer this functionality. In this case, when you need to asserts that the collection does not have duplicate elements, it is advised to use _doesNotHaveDuplicates()_ method:

```java
@Test
public void noDuplicatesTest(){
    List<Integer> numbers = Lists.newArrayList(1, 52, 12, 39, 45, 98, 100, 565, 6, 13);
    assertThat(numbers).doesNotHaveDuplicates();
}
```

### Collection contains the element only once

The extended case of the last situation is to check that element is presented __only once__. Technically, _contains()_ method allows duplicate entities, so to check that element does not repeat, use _containsOnlyOnce()_ assertion.

Observe the code snippet below:

```java
@Test
public void containsOnlyOnceTest(){
    List<Integer> numbers = Lists.newArrayList(1, 1, 52, 12, 12, 45, 45);
    assertThat(numbers).containsOnlyOnce(52);
}
```

## Source code

You can find the full source code for this post in [this github repository](https://github.com/mednikoviurii/mednikov-datastructures). If you have questions regarding this post, don't hesitate to contact me. Have a nice day!