parent
6a7e63795d
commit
f12328011b
|
@ -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"));
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue