Skip to content

Commit 8cbad6d

Browse files
author
p3muet@gmail.com
committed
use TreeMap to guarantee order of keys so that the mismatch description string comparison works everywhere all the time
1 parent dcd8c65 commit 8cbad6d

File tree

3 files changed

+19
-15
lines changed

3 files changed

+19
-15
lines changed

hamcrest-unit-test/src/main/java/org/hamcrest/collection/IsMapContainingKeyTest.java

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
package org.hamcrest.collection;
22

3-
import java.util.HashMap;
4-
import java.util.Map;
3+
import static org.hamcrest.MatcherAssert.assertThat;
4+
import static org.hamcrest.collection.IsMapContainingKey.hasKey;
55

66
import org.hamcrest.AbstractMatcherTest;
77
import org.hamcrest.Matcher;
8-
import static org.hamcrest.MatcherAssert.assertThat;
9-
import static org.hamcrest.collection.IsMapContainingKey.hasKey;
8+
9+
import java.util.HashMap;
10+
import java.util.Map;
11+
import java.util.TreeMap;
1012

1113
public class IsMapContainingKeyTest extends AbstractMatcherTest {
1214

@@ -74,11 +76,11 @@ public void testDoesNotMatchEmptyMap() {
7476
}
7577

7678
public void testDoesNotMatchMapMissingKey() {
77-
Map<String,Integer> map = new HashMap<String, Integer>();
79+
Map<String,Integer> map = new TreeMap<String, Integer>();
7880
map.put("a", 1);
7981
map.put("b", 2);
8082
map.put("c", 3);
8183

82-
assertMismatchDescription("map was [<a=1>, <c=3>, <b=2>]", hasKey("d"), map);
84+
assertMismatchDescription("map was [<a=1>, <b=2>, <c=3>]", hasKey("d"), map);
8385
}
8486
}

hamcrest-unit-test/src/main/java/org/hamcrest/collection/IsMapContainingTest.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
package org.hamcrest.collection;
22

3-
import org.hamcrest.AbstractMatcherTest;
4-
import org.hamcrest.Matcher;
53
import static org.hamcrest.collection.IsMapContaining.hasEntry;
64
import static org.hamcrest.core.IsAnything.anything;
75
import static org.hamcrest.core.IsEqual.equalTo;
86

7+
import org.hamcrest.AbstractMatcherTest;
8+
import org.hamcrest.Matcher;
99

10-
import java.util.HashMap;
1110
import java.util.Map;
11+
import java.util.TreeMap;
1212

1313
public class IsMapContainingTest extends AbstractMatcherTest {
1414

@@ -18,7 +18,7 @@ protected Matcher<?> createMatcher() {
1818
}
1919

2020
public void testMatchesMapContainingMatchingKeyAndValue() {
21-
Map<String,Integer> map = new HashMap<String,Integer>();
21+
Map<String,Integer> map = new TreeMap<String,Integer>();
2222
map.put("a", 1);
2323
map.put("b", 2);
2424

hamcrest-unit-test/src/main/java/org/hamcrest/collection/IsMapContainingValueTest.java

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
package org.hamcrest.collection;
22

3-
import java.util.HashMap;
4-
import java.util.Map;
3+
import static org.hamcrest.collection.IsMapContainingValue.hasValue;
54

65
import org.hamcrest.AbstractMatcherTest;
76
import org.hamcrest.Matcher;
8-
import static org.hamcrest.collection.IsMapContainingValue.hasValue;
7+
8+
import java.util.HashMap;
9+
import java.util.Map;
10+
import java.util.TreeMap;
911

1012
public class IsMapContainingValueTest extends AbstractMatcherTest {
1113

@@ -40,13 +42,13 @@ public void testMatchesSingletonMapContainingValue() {
4042
// }
4143

4244
public void testMatchesMapContainingValue() {
43-
Map<String,Integer> map = new HashMap<String,Integer>();
45+
Map<String,Integer> map = new TreeMap<String,Integer>();
4446
map.put("a", 1);
4547
map.put("b", 2);
4648
map.put("c", 3);
4749

4850
assertMatches("hasValue 1", hasValue(1), map);
4951
assertMatches("hasValue 3", hasValue(3), map);
50-
assertMismatchDescription("map was [<a=1>, <c=3>, <b=2>]", hasValue(4), map);
52+
assertMismatchDescription("map was [<a=1>, <b=2>, <c=3>]", hasValue(4), map);
5153
}
5254
}

0 commit comments

Comments
 (0)