parent
cd9ab90fc8
commit
6a7e63795d
|
@ -4,18 +4,6 @@
|
|||
<modelVersion>4.0.0</modelVersion>
|
||||
<artifactId>testing-assertions</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<configuration>
|
||||
<source>9</source>
|
||||
<target>9</target>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
|
|
|
@ -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<String, Object> innerMap = Map.of("city", "Chicago");
|
||||
Map<String, Map<String, Object>> outerMap = Map.of("address", innerMap);
|
||||
Map<String, Object> innerMap = new HashMap<>();
|
||||
innerMap.put("city", "Chicago");
|
||||
|
||||
Map<String, Map<String, Object>> outerMap = new HashMap<>();
|
||||
outerMap.put("address", innerMap);
|
||||
|
||||
assertTrue(outerMap.containsKey("address") && outerMap.get("address").get("city").equals("Chicago"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenNestedMap_whenUseJupiterAssertAllAndAssertTrue_thenTest() {
|
||||
Map<String, Object> innerMap = Map.of("city", "Chicago");
|
||||
Map<String, Map<String, Object>> outerMap = Map.of("address", innerMap);
|
||||
Map<String, Object> innerMap = new HashMap<>();
|
||||
innerMap.put("city", "Chicago");
|
||||
|
||||
Map<String, Map<String, Object>> 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<String, Object> innerMap = Map.of("city", "Chicago");
|
||||
Map<String, Object> outerMap = Map.of("address", innerMap);
|
||||
Map<String, Object> innerMap = new HashMap<>();
|
||||
innerMap.put("city", "Chicago");
|
||||
|
||||
Map<String, Object> outerMap = new HashMap<>();
|
||||
outerMap.put("address", innerMap);
|
||||
|
||||
assertTrue(outerMap.containsKey("address")
|
||||
&& ((Map<String, Object>)outerMap.get("address")).get("city").equals("Chicago"));
|
||||
|
@ -41,8 +51,12 @@ public class AssertNestedMapUnitTest {
|
|||
|
||||
@Test
|
||||
void givenNestedMap_whenUseHamcrestAssertThat_thenTest() {
|
||||
Map<String, Object> innerMap = Map.of("city", "Chicago");
|
||||
Map<String, Map<String, Object>> outerMap = Map.of("address", innerMap);
|
||||
Map<String, Object> innerMap = new HashMap<>();
|
||||
innerMap.put("city", "Chicago");
|
||||
|
||||
Map<String, Map<String, Object>> 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<String, Object> innerMap = Map.of("city", "Chicago");
|
||||
Map<String, Map<String, Object>> outerMap = Map.of("address", innerMap);
|
||||
Map<String, Object> innerMap = new HashMap<>();
|
||||
innerMap.put("city", "Chicago");
|
||||
|
||||
Map<String, Map<String, Object>> outerMap = new HashMap<>();
|
||||
outerMap.put("address", innerMap);
|
||||
|
||||
assertThat(outerMap, hasEntry(equalTo("address"), hasEntry("city", "Chicago")));
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenNestedMapOfStringAndObject_whenUseHamcrestAssertThatAndCustomMatcher_thenTest() {
|
||||
Map<String, Object> innerMap = Map.of
|
||||
(
|
||||
"city", "Chicago",
|
||||
"zip", "10005"
|
||||
);
|
||||
Map<String, Map<String, Object>> outerMap = Map.of("address", innerMap);
|
||||
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);
|
||||
|
||||
assertThat(outerMap, hasNestedMapEntry("address", innerMap));
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenOuterMapOfStringAndObjectAndInnerMap_whenUseHamcrestAssertThatAndCustomMatcher_thenTest() {
|
||||
Map<String, Object> innerMap = Map.of
|
||||
(
|
||||
"city", "Chicago",
|
||||
"zip", "10005"
|
||||
);
|
||||
Map<String, Object> outerMap = Map.of("address", innerMap);
|
||||
Map<String, Object> innerMap = new HashMap<>();
|
||||
innerMap.put("city", "Chicago");
|
||||
innerMap.put("zip", "10005");
|
||||
|
||||
Map<String, Object> outerMap = new HashMap<>();
|
||||
outerMap.put("address", innerMap);
|
||||
|
||||
assertThat(outerMap, hasNestedMapEntry("address", innerMap));
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenNestedMap_whenUseHamcrestAssertThatWithCasting_thenTest() {
|
||||
Map<String, Object> innerMap = Map.of("city", "Chicago");
|
||||
Map<String, Object> outerMap = Map.of("address", innerMap);
|
||||
Map<String, Object> innerMap = new HashMap<>();
|
||||
innerMap.put("city", "Chicago");
|
||||
Map<String, Object> outerMap = new HashMap<>();
|
||||
outerMap.put("address", innerMap);
|
||||
|
||||
assertThat((Map<String, Object>)outerMap.get("address"), hasEntry("city", "Chicago"));
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue