How do you assert that a collection has a specific size in AssertJ?

How do you assert that a collection has a specific size in AssertJ?

A) hasSize()
B) sizeEquals()
C) isOfSize()
D) containsSize()

Answer:

A) hasSize()

Explanation:

The hasSize() method in AssertJ is used to assert that a collection has a specific size. It verifies that the collection contains the expected number of elements.

For example:


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

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

assertThat(list).hasSize(3);

In this example, the hasSize() method asserts that the list list contains 3 elements.

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