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, used with Java collections. Examples are illustrated using array-based lists, 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:
@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():
@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:
@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 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:
@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:
@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. If you have questions regarding this post, don't hesitate to contact me. Have a nice day!
Top comments (3)
Thanks for the clear explanation. I realized AssertJ a bit late. I remember I spent so much time implementing utilities for these assertions in Junit :)
Hi, Yavuz! Thanks for feedback. I plan to create several posts about AssertJ for different cases: testing date/time, testing Vavr collections.
Great! vavr collections would be interesting, but without converting to Java collections, I guess :)