From c61a1dace10bd3848290088204c36f1d5ed79dc5 Mon Sep 17 00:00:00 2001
From: parthiv39731 <70740707+parthiv39731@users.noreply.github.com>
Date: Mon, 6 Nov 2023 12:24:02 +0530
Subject: [PATCH 1/5] BAEL-7091 Asserting Nested Map with JUnit
---
testing-modules/hamcrest/pom.xml | 12 +++
.../AssertNestedMapUnitTest.java | 94 +++++++++++++++++++
.../matchers/NestedMapMatcher.java | 38 ++++++++
3 files changed, 144 insertions(+)
create mode 100644 testing-modules/hamcrest/src/test/java/com/baeldung/hamcrest/assertnestedmap/AssertNestedMapUnitTest.java
create mode 100644 testing-modules/hamcrest/src/test/java/com/baeldung/hamcrest/assertnestedmap/matchers/NestedMapMatcher.java
diff --git a/testing-modules/hamcrest/pom.xml b/testing-modules/hamcrest/pom.xml
index df8c543edb..b5a8277deb 100644
--- a/testing-modules/hamcrest/pom.xml
+++ b/testing-modules/hamcrest/pom.xml
@@ -6,6 +6,18 @@
hamcrest
0.0.1-SNAPSHOT
hamcrest
+
+
+
+ org.apache.maven.plugins
+ maven-compiler-plugin
+
+
+ 9
+
+
+
+
jar
diff --git a/testing-modules/hamcrest/src/test/java/com/baeldung/hamcrest/assertnestedmap/AssertNestedMapUnitTest.java b/testing-modules/hamcrest/src/test/java/com/baeldung/hamcrest/assertnestedmap/AssertNestedMapUnitTest.java
new file mode 100644
index 0000000000..42c225695c
--- /dev/null
+++ b/testing-modules/hamcrest/src/test/java/com/baeldung/hamcrest/assertnestedmap/AssertNestedMapUnitTest.java
@@ -0,0 +1,94 @@
+package com.baeldung.hamcrest.assertnestedmap;
+
+
+import org.junit.jupiter.api.Test;
+
+import java.util.Map;
+
+import static com.baeldung.hamcrest.assertnestedmap.matchers.NestedMapMatcher.hasNestedMapEntry;
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.Matchers.*;
+import static org.junit.jupiter.api.Assertions.*;
+
+public class AssertNestedMapUnitTest {
+
+ @Test
+ void givenNestedMap_whenUseJupiterAssertTrueWithoutCasting_thenTest() {
+ Map innerMap = Map.of("city", "Chicago");
+ Map> outerMap = Map.of("address", innerMap);
+
+ assertTrue(outerMap.containsKey("address") && outerMap.get("address").get("city").equals("Chicago"));
+ }
+
+ @Test
+ void givenNestedMap_whenUseJupiterAssertAllAndAssertTrue_thenTest() {
+ Map innerMap = Map.of("city", "Chicago");
+ Map> outerMap = Map.of("address", innerMap);
+
+ assertAll(
+ () -> assertTrue(outerMap.containsKey("address")),
+ () -> assertEquals(outerMap.get("address").get("city"), "Chicago")
+ );
+ }
+
+ @Test
+ void givenNestedMap_whenUseJupiterAssertTrueWithCasting_thenTest() {
+ Map innerMap = Map.of("city", "Chicago");
+ Map outerMap = Map.of("address", innerMap);
+
+ assertTrue(outerMap.containsKey("address")
+ && ((Map)outerMap.get("address")).get("city").equals("Chicago"));
+ }
+
+ @Test
+ void givenNestedMap_whenUseHamcrestAssertThat_thenTest() {
+ Map innerMap = Map.of("city", "Chicago");
+ Map> outerMap = Map.of("address", innerMap);
+ assertAll(
+ () -> assertThat(outerMap, hasKey("address")),
+ () -> assertThat(outerMap.get("address"), hasEntry("city", "Chicago"))
+ );
+ }
+
+ @Test
+ void givenNestedMapOfStringAndObject_whenUseHamcrestAssertThat_thenTest() {
+ Map innerMap = Map.of("city", "Chicago");
+ Map> outerMap = Map.of("address", innerMap);
+
+ assertThat(outerMap, hasEntry(equalTo("address"), hasEntry("city", "Chicago")));
+ }
+
+ @Test
+ void givenNestedMapOfStringAndObject_whenUseHamcrestAssertThatAndCustomMatcher_thenTest() {
+ Map innerMap = Map.of
+ (
+ "city", "Chicago",
+ "zip", "10005"
+ );
+ Map> outerMap = Map.of("address", innerMap);
+
+ assertThat(outerMap, hasNestedMapEntry("address", innerMap));
+ }
+
+ @Test
+ void givenOuterMapOfStringAndObjectAndInnerMap_whenUseHamcrestAssertThatAndCustomMatcher_thenTest() {
+ Map innerMap = Map.of
+ (
+ "city", "Chicago",
+ "zip", "10005"
+ );
+ Map outerMap = Map.of("address", innerMap);
+
+ assertThat(outerMap, hasNestedMapEntry("address", innerMap));
+ }
+
+
+ @Test
+ void givenNestedMap_whenUseHamcrestAssertThatWithCasting_thenTest() {
+ Map innerMap = Map.of("city", "Chicago");
+ Map outerMap = Map.of("address", innerMap);
+
+ assertThat((Map)outerMap.get("address"), hasEntry("city", "Chicago"));
+ }
+
+}
diff --git a/testing-modules/hamcrest/src/test/java/com/baeldung/hamcrest/assertnestedmap/matchers/NestedMapMatcher.java b/testing-modules/hamcrest/src/test/java/com/baeldung/hamcrest/assertnestedmap/matchers/NestedMapMatcher.java
new file mode 100644
index 0000000000..760edebb35
--- /dev/null
+++ b/testing-modules/hamcrest/src/test/java/com/baeldung/hamcrest/assertnestedmap/matchers/NestedMapMatcher.java
@@ -0,0 +1,38 @@
+package com.baeldung.hamcrest.assertnestedmap.matchers;
+
+
+import org.hamcrest.Description;
+import org.hamcrest.Matcher;
+import org.hamcrest.TypeSafeMatcher;
+
+import java.util.Map;
+
+public class NestedMapMatcher extends TypeSafeMatcher