BAEL-7091

reverted Java 9 snippets
This commit is contained in:
parthiv39731 2023-11-16 20:10:00 +05:30
parent 6a7e63795d
commit f12328011b
1 changed files with 25 additions and 43 deletions

View File

@ -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<String, Object> innerMap = new HashMap<>();
innerMap.put("city", "Chicago");
Map<String, Map<String, Object>> outerMap = new HashMap<>();
outerMap.put("address", innerMap);
Map<String, Object> innerMap = Map.of("city", "Chicago");
Map<String, Map<String, Object>> outerMap = Map.of("address", innerMap);
assertTrue(outerMap.containsKey("address") && outerMap.get("address").get("city").equals("Chicago"));
}
@Test
void givenNestedMap_whenUseJupiterAssertAllAndAssertTrue_thenTest() {
Map<String, Object> innerMap = new HashMap<>();
innerMap.put("city", "Chicago");
Map<String, Map<String, Object>> outerMap = new HashMap<>();
outerMap.put("address", innerMap);
Map<String, Object> innerMap = Map.of("city", "Chicago");
Map<String, Map<String, Object>> outerMap = Map.of("address", innerMap);
assertAll(
() -> assertTrue(outerMap.containsKey("address")),
@ -39,11 +32,8 @@ public class AssertNestedMapUnitTest {
@Test
void givenNestedMap_whenUseJupiterAssertTrueWithCasting_thenTest() {
Map<String, Object> innerMap = new HashMap<>();
innerMap.put("city", "Chicago");
Map<String, Object> outerMap = new HashMap<>();
outerMap.put("address", innerMap);
Map<String, Object> innerMap = Map.of("city", "Chicago");
Map<String, Object> outerMap = Map.of("address", innerMap);
assertTrue(outerMap.containsKey("address")
&& ((Map<String, Object>)outerMap.get("address")).get("city").equals("Chicago"));
@ -51,12 +41,8 @@ public class AssertNestedMapUnitTest {
@Test
void givenNestedMap_whenUseHamcrestAssertThat_thenTest() {
Map<String, Object> innerMap = new HashMap<>();
innerMap.put("city", "Chicago");
Map<String, Map<String, Object>> outerMap = new HashMap<>();
outerMap.put("address", innerMap);
Map<String, Object> innerMap = Map.of("city", "Chicago");
Map<String, Map<String, Object>> 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<String, Object> innerMap = new HashMap<>();
innerMap.put("city", "Chicago");
Map<String, Map<String, Object>> outerMap = new HashMap<>();
outerMap.put("address", innerMap);
Map<String, Object> innerMap = Map.of("city", "Chicago");
Map<String, Map<String, Object>> outerMap = Map.of("address", innerMap);
assertThat(outerMap, hasEntry(equalTo("address"), hasEntry("city", "Chicago")));
}
@Test
void givenNestedMapOfStringAndObject_whenUseHamcrestAssertThatAndCustomMatcher_thenTest() {
Map<String, Object> innerMap = new HashMap<>();
innerMap.put("city", "Chicago");
innerMap.put("zip", "10005");
Map<String, Map<String, Object>> outerMap = new HashMap<>();
outerMap.put("address", innerMap);
Map<String, Object> innerMap = Map.of
(
"city", "Chicago",
"zip", "10005"
);
Map<String, Map<String, Object>> outerMap = Map.of("address", innerMap);
assertThat(outerMap, hasNestedMapEntry("address", innerMap));
}
@Test
void givenOuterMapOfStringAndObjectAndInnerMap_whenUseHamcrestAssertThatAndCustomMatcher_thenTest() {
Map<String, Object> innerMap = new HashMap<>();
innerMap.put("city", "Chicago");
innerMap.put("zip", "10005");
Map<String, Object> outerMap = new HashMap<>();
outerMap.put("address", innerMap);
Map<String, Object> innerMap = Map.of
(
"city", "Chicago",
"zip", "10005"
);
Map<String, Object> outerMap = Map.of("address", innerMap);
assertThat(outerMap, hasNestedMapEntry("address", innerMap));
}
@Test
void givenNestedMap_whenUseHamcrestAssertThatWithCasting_thenTest() {
Map<String, Object> innerMap = new HashMap<>();
innerMap.put("city", "Chicago");
Map<String, Object> outerMap = new HashMap<>();
outerMap.put("address", innerMap);
Map<String, Object> innerMap = Map.of("city", "Chicago");
Map<String, Object> outerMap = Map.of("address", innerMap);
assertThat((Map<String, Object>)outerMap.get("address"), hasEntry("city", "Chicago"));
}
}