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”.