AssertJ provides dedicated assertions for Map types, covering key presence, value correctness, full entry matching, and size checks. When assertThat() receives a Map, it returns a MapAssert with all these methods available.
entry() helper
The static entry(key, value) method from Assertions creates a Map.Entry for use in assertions, making it easy to check exact key-value pairs without constructing entries manually.
Key Map Assertion Methods
containsKey, doesNotContainKey, containsValue, containsEntry, contains (with entry()), hasSize, isEmpty.
Example
package com.logicbig.example;
import org.junit.jupiter.api.Test;
import java.util.Map;
import static org.assertj.core.api.Assertions.*;
public class MapAssertionsExample {
@Test
void myTest() {
Map<String, Integer> scores = Map.of("Alice",
95,
"Bob",
80,
"Charlie",
70);
// key checks
assertThat(scores).containsKey("Alice")
.doesNotContainKey("Dave");
System.out.println("Key checks passed");
// value checks
assertThat(scores).containsValue(95)
.doesNotContainValue(100);
System.out.println("Value checks passed");
// exact entry
assertThat(scores).containsEntry("Bob", 80);
System.out.println("Entry check passed");
// using entry() helper
assertThat(scores).contains(entry("Alice", 95), entry("Charlie", 70));
System.out.println("entry() helper check passed");
// size
assertThat(scores).hasSize(3);
System.out.println("Size check passed");
}
}
Output$ mvn clean test -Dtest=* [INFO] Scanning for projects... [INFO] [INFO] ------------< com.logicbig.example:assertj-map-assertions >------------- [INFO] Building assertj-map-assertions 1.0-SNAPSHOT [INFO] from pom.xml [INFO] --------------------------------[ jar ]--------------------------------- [INFO] [INFO] --- clean:3.2.0:clean (default-clean) @ assertj-map-assertions --- [INFO] [INFO] --- resources:3.3.1:resources (default-resources) @ assertj-map-assertions --- [INFO] skip non existing resourceDirectory D:\example-projects\assertj\assertj-map-assertions\src\main\resources [INFO] [INFO] --- compiler:3.11.0:compile (default-compile) @ assertj-map-assertions --- [INFO] No sources to compile [INFO] [INFO] --- resources:3.3.1:testResources (default-testResources) @ assertj-map-assertions --- [INFO] skip non existing resourceDirectory D:\example-projects\assertj\assertj-map-assertions\src\test\resources [INFO] [INFO] --- compiler:3.11.0:testCompile (default-testCompile) @ assertj-map-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-map-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.MapAssertionsExample Key checks passed Value checks passed Entry check passed entry() helper check passed Size check passed [INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.086 s -- in com.logicbig.example.MapAssertionsExample [INFO] [INFO] Results: [INFO] [INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0 [INFO] [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 2.684 s [INFO] Finished at: 2026-03-03T13:35:07+08:00 [INFO] ------------------------------------------------------------------------
Conclusion
The output confirms each map assertion passes: key presence, value lookup, exact entry matching using entry(), and absence checks all work as expected. AssertJ's map assertions make it straightforward to validate configuration maps, caches, or any key-value output from your 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
|
|