When testing collection-based results, AssertJ provides dedicated assertion methods for Iterable, List, Set, and arrays. These go far beyond a simple size check — you can verify element presence, order, uniqueness, and even filter or extract elements before asserting.
Why These Assertions Matter
Collections are central to most Java applications. Verifying that a service returns the correct items, in the correct order, without unwanted entries, is a common test requirement. AssertJ's collection assertions let you express these checks concisely and fail with clear, descriptive messages.
Key Methods
hasSize, contains, containsExactly, containsOnly, doesNotContain, isEmpty, isNotEmpty, hasDuplicates, isSortedAccordingTo.
Example
package com.logicbig.example;
import org.junit.jupiter.api.Test;
import java.util.List;
import java.util.Set;
import static org.assertj.core.api.Assertions.*;
public class CollectionAssertionsExample {
@Test
void myTest() {
List<String> fruits = List.of("apple", "banana", "cherry");
// size and presence
assertThat(fruits).hasSize(3)
.contains("banana")
.doesNotContain("mango");
System.out.println("Size and presence passed");
// exact order
assertThat(fruits).containsExactly("apple", "banana", "cherry");
System.out.println("Exact order check passed");
// containsOnly: any order, no extras
assertThat(fruits).containsOnly("cherry", "apple", "banana");
System.out.println("containsOnly passed");
// Set: no duplicates
Set<Integer> nums = Set.of(1, 2, 3);
assertThat(nums).isNotEmpty()
.hasSize(3);
System.out.println("Set assertion passed");
}
}
Output$ mvn clean test -Dtest=* [INFO] Scanning for projects... [INFO] [INFO] ---------< com.logicbig.example:assertj-collection-assertions >--------- [INFO] Building assertj-collection-assertions 1.0-SNAPSHOT [INFO] from pom.xml [INFO] --------------------------------[ jar ]--------------------------------- [INFO] [INFO] --- clean:3.2.0:clean (default-clean) @ assertj-collection-assertions --- [INFO] [INFO] --- resources:3.3.1:resources (default-resources) @ assertj-collection-assertions --- [INFO] skip non existing resourceDirectory D:\example-projects\assertj\assertj-collection-assertions\src\main\resources [INFO] [INFO] --- compiler:3.11.0:compile (default-compile) @ assertj-collection-assertions --- [INFO] No sources to compile [INFO] [INFO] --- resources:3.3.1:testResources (default-testResources) @ assertj-collection-assertions --- [INFO] skip non existing resourceDirectory D:\example-projects\assertj\assertj-collection-assertions\src\test\resources [INFO] [INFO] --- compiler:3.11.0:testCompile (default-testCompile) @ assertj-collection-assertions --- [INFO] Changes detected - recompiling the module! :source [INFO] Compiling 1 source file with javac [debug target 17] to target\test-classes [INFO] [INFO] --- surefire:3.2.5:test (default-test) @ assertj-collection-assertions --- [INFO] Using auto detected provider org.apache.maven.surefire.junitplatform.JUnitPlatformProvider [WARNING] file.encoding cannot be set as system property, use <argLine>-Dfile.encoding=...</argLine> instead [INFO] [INFO] ------------------------------------------------------- [INFO] T E S T S [INFO] ------------------------------------------------------- [INFO] Running com.logicbig.example.CollectionAssertionsExample Size and presence passed Exact order check passed containsOnly passed Set assertion passed [INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.156 s -- in com.logicbig.example.CollectionAssertionsExample [INFO] [INFO] Results: [INFO] [INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0 [INFO] [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 5.665 s [INFO] Finished at: 2026-03-03T11:06:47+08:00 [INFO] ------------------------------------------------------------------------
Conclusion
The output shows all collection assertions pass. The example illustrates how contains checks for element presence regardless of order, while containsExactly enforces strict ordering — choosing the right method makes intentions explicit in the test code.
Example ProjectDependencies and Technologies Used: - assertj-core 3.27.7 (Rich and fluent assertions for testing in Java)
- junit-jupiter-engine 6.0.2 (Module "junit-jupiter-engine" of JUnit)
- JDK 17
- Maven 3.9.11
|
|