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 + 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> { + private K key; + private V subMapValue; + + public NestedMapMatcher(K key, V subMapValue) { + this.key = key; + this.subMapValue = subMapValue; + } + + @Override + protected boolean matchesSafely(Map item) { + if (item.containsKey(key)) { + Object actualValue = item.get(key); + return subMapValue.equals(actualValue); + } + return false; + } + + @Override + public void describeTo(Description description) { + description.appendText("a map containing key ").appendValue(key) + .appendText(" with value ").appendValue(subMapValue); + } + + public static Matcher hasNestedMapEntry(K key, V expectedValue) { + return new NestedMapMatcher(key, expectedValue); + } +} + From e9431ce416773b07ea3efc42bc1a4cbaa783114a Mon Sep 17 00:00:00 2001 From: parthiv39731 <70740707+parthiv39731@users.noreply.github.com> Date: Mon, 6 Nov 2023 19:27:07 +0530 Subject: [PATCH 2/5] BAEL-7091 Formatting fixed --- .../assertnestedmap/AssertNestedMapUnitTest.java | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) 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 index 42c225695c..b5b4d652a0 100644 --- 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 @@ -11,7 +11,6 @@ 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"); @@ -26,8 +25,8 @@ public class AssertNestedMapUnitTest { Map> outerMap = Map.of("address", innerMap); assertAll( - () -> assertTrue(outerMap.containsKey("address")), - () -> assertEquals(outerMap.get("address").get("city"), "Chicago") + () -> assertTrue(outerMap.containsKey("address")), + () -> assertEquals(outerMap.get("address").get("city"), "Chicago") ); } @@ -45,8 +44,8 @@ public class AssertNestedMapUnitTest { Map innerMap = Map.of("city", "Chicago"); Map> outerMap = Map.of("address", innerMap); assertAll( - () -> assertThat(outerMap, hasKey("address")), - () -> assertThat(outerMap.get("address"), hasEntry("city", "Chicago")) + () -> assertThat(outerMap, hasKey("address")), + () -> assertThat(outerMap.get("address"), hasEntry("city", "Chicago")) ); } @@ -82,7 +81,6 @@ public class AssertNestedMapUnitTest { assertThat(outerMap, hasNestedMapEntry("address", innerMap)); } - @Test void givenNestedMap_whenUseHamcrestAssertThatWithCasting_thenTest() { Map innerMap = Map.of("city", "Chicago"); From cd9ab90fc857627079b9f57c7fc894fdd63f8669 Mon Sep 17 00:00:00 2001 From: parthiv39731 <70740707+parthiv39731@users.noreply.github.com> Date: Fri, 10 Nov 2023 20:51:43 +0530 Subject: [PATCH 3/5] BAEL-7091 Moved to new module --- testing-modules/hamcrest/pom.xml | 12 ------------ testing-modules/testing-assertions/pom.xml | 12 ++++++++++++ .../assertnestedmap/AssertNestedMapUnitTest.java | 4 ++-- .../assertnestedmap/matchers/NestedMapMatcher.java | 2 +- 4 files changed, 15 insertions(+), 15 deletions(-) rename testing-modules/{hamcrest/src/test/java/com/baeldung/hamcrest => testing-assertions/src/test/java/com/baeldung}/assertnestedmap/AssertNestedMapUnitTest.java (95%) rename testing-modules/{hamcrest/src/test/java/com/baeldung/hamcrest => testing-assertions/src/test/java/com/baeldung}/assertnestedmap/matchers/NestedMapMatcher.java (94%) diff --git a/testing-modules/hamcrest/pom.xml b/testing-modules/hamcrest/pom.xml index b5a8277deb..df8c543edb 100644 --- a/testing-modules/hamcrest/pom.xml +++ b/testing-modules/hamcrest/pom.xml @@ -6,18 +6,6 @@ hamcrest 0.0.1-SNAPSHOT hamcrest - - - - org.apache.maven.plugins - maven-compiler-plugin - - 9 - 9 - - - - jar diff --git a/testing-modules/testing-assertions/pom.xml b/testing-modules/testing-assertions/pom.xml index 1da53bd77e..1f2b4e335d 100644 --- a/testing-modules/testing-assertions/pom.xml +++ b/testing-modules/testing-assertions/pom.xml @@ -4,6 +4,18 @@ 4.0.0 testing-assertions 0.0.1-SNAPSHOT + + + + org.apache.maven.plugins + maven-compiler-plugin + + 9 + 9 + + + + com.baeldung diff --git a/testing-modules/hamcrest/src/test/java/com/baeldung/hamcrest/assertnestedmap/AssertNestedMapUnitTest.java b/testing-modules/testing-assertions/src/test/java/com/baeldung/assertnestedmap/AssertNestedMapUnitTest.java similarity index 95% rename from testing-modules/hamcrest/src/test/java/com/baeldung/hamcrest/assertnestedmap/AssertNestedMapUnitTest.java rename to testing-modules/testing-assertions/src/test/java/com/baeldung/assertnestedmap/AssertNestedMapUnitTest.java index b5b4d652a0..7c48a5eb4a 100644 --- a/testing-modules/hamcrest/src/test/java/com/baeldung/hamcrest/assertnestedmap/AssertNestedMapUnitTest.java +++ b/testing-modules/testing-assertions/src/test/java/com/baeldung/assertnestedmap/AssertNestedMapUnitTest.java @@ -1,11 +1,11 @@ -package com.baeldung.hamcrest.assertnestedmap; +package com.baeldung.assertnestedmap; import org.junit.jupiter.api.Test; import java.util.Map; -import static com.baeldung.hamcrest.assertnestedmap.matchers.NestedMapMatcher.hasNestedMapEntry; +import static com.baeldung.assertnestedmap.matchers.NestedMapMatcher.hasNestedMapEntry; import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.*; import static org.junit.jupiter.api.Assertions.*; diff --git a/testing-modules/hamcrest/src/test/java/com/baeldung/hamcrest/assertnestedmap/matchers/NestedMapMatcher.java b/testing-modules/testing-assertions/src/test/java/com/baeldung/assertnestedmap/matchers/NestedMapMatcher.java similarity index 94% rename from testing-modules/hamcrest/src/test/java/com/baeldung/hamcrest/assertnestedmap/matchers/NestedMapMatcher.java rename to testing-modules/testing-assertions/src/test/java/com/baeldung/assertnestedmap/matchers/NestedMapMatcher.java index 760edebb35..961130bb20 100644 --- a/testing-modules/hamcrest/src/test/java/com/baeldung/hamcrest/assertnestedmap/matchers/NestedMapMatcher.java +++ b/testing-modules/testing-assertions/src/test/java/com/baeldung/assertnestedmap/matchers/NestedMapMatcher.java @@ -1,4 +1,4 @@ -package com.baeldung.hamcrest.assertnestedmap.matchers; +package com.baeldung.assertnestedmap.matchers; import org.hamcrest.Description; From 6a7e63795d0f332c00eb2d06699da87dc94adca3 Mon Sep 17 00:00:00 2001 From: parthiv39731 <70740707+parthiv39731@users.noreply.github.com> Date: Wed, 15 Nov 2023 11:15:27 +0530 Subject: [PATCH 4/5] BAEL-7091 removed Java 9 snippets --- testing-modules/testing-assertions/pom.xml | 12 ---- .../AssertNestedMapUnitTest.java | 68 ++++++++++++------- 2 files changed, 43 insertions(+), 37 deletions(-) diff --git a/testing-modules/testing-assertions/pom.xml b/testing-modules/testing-assertions/pom.xml index 1f2b4e335d..1da53bd77e 100644 --- a/testing-modules/testing-assertions/pom.xml +++ b/testing-modules/testing-assertions/pom.xml @@ -4,18 +4,6 @@ 4.0.0 testing-assertions 0.0.1-SNAPSHOT - - - - org.apache.maven.plugins - maven-compiler-plugin - - 9 - 9 - - - - com.baeldung diff --git a/testing-modules/testing-assertions/src/test/java/com/baeldung/assertnestedmap/AssertNestedMapUnitTest.java b/testing-modules/testing-assertions/src/test/java/com/baeldung/assertnestedmap/AssertNestedMapUnitTest.java index 7c48a5eb4a..ceef5efdd2 100644 --- a/testing-modules/testing-assertions/src/test/java/com/baeldung/assertnestedmap/AssertNestedMapUnitTest.java +++ b/testing-modules/testing-assertions/src/test/java/com/baeldung/assertnestedmap/AssertNestedMapUnitTest.java @@ -3,6 +3,7 @@ package com.baeldung.assertnestedmap; import org.junit.jupiter.api.Test; +import java.util.HashMap; import java.util.Map; import static com.baeldung.assertnestedmap.matchers.NestedMapMatcher.hasNestedMapEntry; @@ -13,16 +14,22 @@ 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); + Map innerMap = new HashMap<>(); + innerMap.put("city", "Chicago"); + + Map> outerMap = new HashMap<>(); + outerMap.put("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); + Map innerMap = new HashMap<>(); + innerMap.put("city", "Chicago"); + + Map> outerMap = new HashMap<>(); + outerMap.put("address", innerMap); assertAll( () -> assertTrue(outerMap.containsKey("address")), @@ -32,8 +39,11 @@ public class AssertNestedMapUnitTest { @Test void givenNestedMap_whenUseJupiterAssertTrueWithCasting_thenTest() { - Map innerMap = Map.of("city", "Chicago"); - Map outerMap = Map.of("address", innerMap); + Map innerMap = new HashMap<>(); + innerMap.put("city", "Chicago"); + + Map outerMap = new HashMap<>(); + outerMap.put("address", innerMap); assertTrue(outerMap.containsKey("address") && ((Map)outerMap.get("address")).get("city").equals("Chicago")); @@ -41,8 +51,12 @@ public class AssertNestedMapUnitTest { @Test void givenNestedMap_whenUseHamcrestAssertThat_thenTest() { - Map innerMap = Map.of("city", "Chicago"); - Map> outerMap = Map.of("address", innerMap); + Map innerMap = new HashMap<>(); + innerMap.put("city", "Chicago"); + + Map> outerMap = new HashMap<>(); + outerMap.put("address", innerMap); + assertAll( () -> assertThat(outerMap, hasKey("address")), () -> assertThat(outerMap.get("address"), hasEntry("city", "Chicago")) @@ -51,42 +65,46 @@ public class AssertNestedMapUnitTest { @Test void givenNestedMapOfStringAndObject_whenUseHamcrestAssertThat_thenTest() { - Map innerMap = Map.of("city", "Chicago"); - Map> outerMap = Map.of("address", innerMap); + Map innerMap = new HashMap<>(); + innerMap.put("city", "Chicago"); + + Map> outerMap = new HashMap<>(); + outerMap.put("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); + Map innerMap = new HashMap<>(); + innerMap.put("city", "Chicago"); + innerMap.put("zip", "10005"); + + Map> outerMap = new HashMap<>(); + outerMap.put("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); + Map innerMap = new HashMap<>(); + innerMap.put("city", "Chicago"); + innerMap.put("zip", "10005"); + + Map outerMap = new HashMap<>(); + outerMap.put("address", innerMap); assertThat(outerMap, hasNestedMapEntry("address", innerMap)); } @Test void givenNestedMap_whenUseHamcrestAssertThatWithCasting_thenTest() { - Map innerMap = Map.of("city", "Chicago"); - Map outerMap = Map.of("address", innerMap); + Map innerMap = new HashMap<>(); + innerMap.put("city", "Chicago"); + Map outerMap = new HashMap<>(); + outerMap.put("address", innerMap); assertThat((Map)outerMap.get("address"), hasEntry("city", "Chicago")); } - } From f12328011b0a0c254b5096c3ce0dbfd7dcbd4c7f Mon Sep 17 00:00:00 2001 From: parthiv39731 <70740707+parthiv39731@users.noreply.github.com> Date: Thu, 16 Nov 2023 20:10:00 +0530 Subject: [PATCH 5/5] BAEL-7091 reverted Java 9 snippets --- .../AssertNestedMapUnitTest.java | 68 +++++++------------ 1 file changed, 25 insertions(+), 43 deletions(-) diff --git a/testing-modules/testing-assertions/src/test/java/com/baeldung/assertnestedmap/AssertNestedMapUnitTest.java b/testing-modules/testing-assertions/src/test/java/com/baeldung/assertnestedmap/AssertNestedMapUnitTest.java index ceef5efdd2..7c48a5eb4a 100644 --- a/testing-modules/testing-assertions/src/test/java/com/baeldung/assertnestedmap/AssertNestedMapUnitTest.java +++ b/testing-modules/testing-assertions/src/test/java/com/baeldung/assertnestedmap/AssertNestedMapUnitTest.java @@ -3,7 +3,6 @@ package com.baeldung.assertnestedmap; import org.junit.jupiter.api.Test; -import java.util.HashMap; import java.util.Map; import static com.baeldung.assertnestedmap.matchers.NestedMapMatcher.hasNestedMapEntry; @@ -14,22 +13,16 @@ import static org.junit.jupiter.api.Assertions.*; public class AssertNestedMapUnitTest { @Test void givenNestedMap_whenUseJupiterAssertTrueWithoutCasting_thenTest() { - Map innerMap = new HashMap<>(); - innerMap.put("city", "Chicago"); - - Map> outerMap = new HashMap<>(); - outerMap.put("address", innerMap); + 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 = new HashMap<>(); - innerMap.put("city", "Chicago"); - - Map> outerMap = new HashMap<>(); - outerMap.put("address", innerMap); + Map innerMap = Map.of("city", "Chicago"); + Map> outerMap = Map.of("address", innerMap); assertAll( () -> assertTrue(outerMap.containsKey("address")), @@ -39,11 +32,8 @@ public class AssertNestedMapUnitTest { @Test void givenNestedMap_whenUseJupiterAssertTrueWithCasting_thenTest() { - Map innerMap = new HashMap<>(); - innerMap.put("city", "Chicago"); - - Map outerMap = new HashMap<>(); - outerMap.put("address", innerMap); + Map innerMap = Map.of("city", "Chicago"); + Map outerMap = Map.of("address", innerMap); assertTrue(outerMap.containsKey("address") && ((Map)outerMap.get("address")).get("city").equals("Chicago")); @@ -51,12 +41,8 @@ public class AssertNestedMapUnitTest { @Test void givenNestedMap_whenUseHamcrestAssertThat_thenTest() { - Map innerMap = new HashMap<>(); - innerMap.put("city", "Chicago"); - - Map> outerMap = new HashMap<>(); - outerMap.put("address", innerMap); - + Map innerMap = Map.of("city", "Chicago"); + Map> outerMap = Map.of("address", innerMap); assertAll( () -> assertThat(outerMap, hasKey("address")), () -> assertThat(outerMap.get("address"), hasEntry("city", "Chicago")) @@ -65,46 +51,42 @@ public class AssertNestedMapUnitTest { @Test void givenNestedMapOfStringAndObject_whenUseHamcrestAssertThat_thenTest() { - Map innerMap = new HashMap<>(); - innerMap.put("city", "Chicago"); - - Map> outerMap = new HashMap<>(); - outerMap.put("address", innerMap); + 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 = new HashMap<>(); - innerMap.put("city", "Chicago"); - innerMap.put("zip", "10005"); - - Map> outerMap = new HashMap<>(); - outerMap.put("address", innerMap); + 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 = new HashMap<>(); - innerMap.put("city", "Chicago"); - innerMap.put("zip", "10005"); - - Map outerMap = new HashMap<>(); - outerMap.put("address", innerMap); + 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 = new HashMap<>(); - innerMap.put("city", "Chicago"); - Map outerMap = new HashMap<>(); - outerMap.put("address", innerMap); + Map innerMap = Map.of("city", "Chicago"); + Map outerMap = Map.of("address", innerMap); assertThat((Map)outerMap.get("address"), hasEntry("city", "Chicago")); } + }