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); + } +} +