Difference Between hasItems(), contains(), and containsInAnyOrder() in Hamcrest

Yatin B.Yatin B.
1 min read

Date: 2024-08-14

Hamcrest's hasItems(), contains(), and containsInAnyOrder() matchers offer distinct ways to assert collection contents in unit tests. hasItems() checks for element presence regardless of order or duplicates. contains() verifies both order and exact element count, including duplicates. containsInAnyOrder() ignores order but requires an exact element count. The choice depends on the specific validation needs; use hasItems() for presence only, contains() for strict order and count, and containsInAnyOrder() for flexible order but precise count.

Read more: https://www.javacodegeeks.com/hasitems-vs-contains-vs-containsinanyorder-in-hamcrest.html

0
Subscribe to my newsletter

Read articles from Yatin B. directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Yatin B.
Yatin B.