File tree 2 files changed +46
-1
lines changed
hamcrest/src/test/java/org/hamcrest/collection
2 files changed +46
-1
lines changed Original file line number Diff line number Diff line change 4
4
import org .hamcrest .Matcher ;
5
5
6
6
import java .util .HashMap ;
7
+ import java .util .Hashtable ;
7
8
import java .util .Map ;
8
9
import java .util .TreeMap ;
9
10
@@ -62,7 +63,25 @@ public void testMatchesMapContainingKeyWithNumberKeys() throws Exception {
62
63
assertThat (map , hasKey ((Number )1 ));
63
64
64
65
// TODO: work out the correct sprinkling of wildcards to get this to work!
65
- // assertThat(map, hasKey(1));
66
+ // assertThat(map, hasKey(1));
67
+ }
68
+
69
+ public void test_mapContainingNullKey_returnsFalse_forMapsWithNonnullKeys () throws Exception {
70
+ Map <Number , String > map = new Hashtable <>(); // Hashtables cannot store null keys
71
+ map .put (1 , "A" );
72
+ map .put (2 , "B" );
73
+
74
+ assertDoesNotMatch (hasKey ((Number )null ), map );
75
+ }
76
+
77
+
78
+ public void test_mapContainingNullKey_returnsTrue_forMapWithNullKey () throws Exception {
79
+ Map <Number , String > map = new HashMap <>(); // HashMap can store null keys
80
+ map .put (1 , "A" );
81
+ map .put (2 , "B" );
82
+ map .put (null , "C" );
83
+
84
+ assertMatches (hasKey ((Number )null ), map );
66
85
}
67
86
68
87
public void testHasReadableDescription () {
Original file line number Diff line number Diff line change 4
4
import org .hamcrest .Matcher ;
5
5
6
6
import java .util .HashMap ;
7
+ import java .util .Hashtable ;
7
8
import java .util .Map ;
8
9
import java .util .TreeMap ;
9
10
@@ -46,4 +47,29 @@ public void testDoesNotMatchNull() {
46
47
public void testHasReadableDescription () {
47
48
assertDescription ("map containing [\" a\" -><2>]" , hasEntry (equalTo ("a" ), (equalTo (2 ))));
48
49
}
50
+
51
+ public void test_mapContainsEntryWithNullKey_returnsFalseForMapsWithoutNullKeys (){
52
+ Map <String , Integer > map = new Hashtable <>(); // throws exception if given null key, or if map.containsKey(null) is called
53
+ map .put ("a" , 1 );
54
+ map .put ("b" , 2 );
55
+
56
+ assertDoesNotMatch (hasEntry (null , 2 ), map );
57
+ }
58
+
59
+ public void test_mapContainsEntryWithNullKey_returnsTrueForMapWithNullKeyAndMatchingValue (){
60
+ Map <String , Integer > map = new HashMap <>(); // throws exception if given null key, or if map.containsKey(null) is called
61
+ map .put ("a" , 1 );
62
+ map .put ("b" , 2 );
63
+ map .put (null , 3 );
64
+
65
+ assertMatches (hasEntry (null , 3 ), map );
66
+ }
67
+
68
+ public void test_mapContainsEntryWithNullKey_returnsFalseForMapWithNullKeyAndNoMatchingValue (){
69
+ Map <String , Integer > map = new HashMap <>(); // throws exception if given null key, or if map.containsKey(null) is called
70
+ map .put ("a" , 1 );
71
+ map .put ("b" , 2 );
72
+
73
+ assertDoesNotMatch (hasEntry (null , 3 ), map );
74
+ }
49
75
}
You can’t perform that action at this time.
0 commit comments