How do you assert that a list contains specific elements in AssertJ?

How do you assert that a list contains specific elements in AssertJ?

A) contains()
B) hasItems()
C) includes()
D) hasElements()

Answer:

A) contains()

Explanation:

The contains() method in AssertJ is used to assert that a list contains specific elements. It allows you to verify that a list includes the given elements, in the specified order.

For example:


import static org.assertj.core.api.Assertions.assertThat;

List<String> list = Arrays.asList("apple", "banana", "cherry");

assertThat(list).contains("banana", "cherry");

In this example, the contains() method is used to assert that the list contains the elements “banana” and “cherry”.

Reference links:

https://www.javaguides.net/p/top-java-libraries.html

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top