Skip to content

Commit

Permalink
Fix NPE in MapSubject.containsExactly et al when a key is present wit…
Browse files Browse the repository at this point in the history
…h the wrong value, and the actual or expected value is null.

RELNOTES=n/a

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=201697457
  • Loading branch information
peteg authored and cgdecker committed Jun 28, 2018
1 parent b974c81 commit bf2cc0d
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
3 changes: 2 additions & 1 deletion core/src/main/java/com/google/common/truth/MapSubject.java
Original file line number Diff line number Diff line change
Expand Up @@ -348,7 +348,8 @@ private static class ValueDifference<A, E> {
new Function<ValueDifference<Object, Object>, String>() {
@Override
public String apply(ValueDifference<Object, Object> values) {
boolean includeTypes = values.actual.toString().equals(values.expected.toString());
boolean includeTypes =
String.valueOf(values.actual).equals(String.valueOf(values.expected));
return lenientFormat(
"(expected %s but got %s)",
includeTypes ? new TypedToStringWrapper(values.expected) : values.expected,
Expand Down
13 changes: 13 additions & 0 deletions core/src/test/java/com/google/common/truth/MapSubjectTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,19 @@ public void containsExactlyWrongValue() {
+ "{march=(expected 33 but got 3)}");
}

@Test
public void containsExactlyWrongValueWithNull() {
// Test for https://github.com/google/truth/issues/468
ImmutableMap<String, Integer> actual = ImmutableMap.of("jan", 1, "feb", 2, "march", 3);
expectFailureWhenTestingThat(actual).containsExactly("jan", 1, "march", null, "feb", 2);
assertThat(expectFailure.getFailure())
.hasMessageThat()
.isEqualTo(
"Not true that <{jan=1, feb=2, march=3}> contains exactly "
+ "<{jan=1, march=null, feb=2}>. It has the following entries with matching keys "
+ "but different values: {march=(expected null but got 3)}");
}

@Test
public void containsExactlyExtraKeyAndMissingKey() {
ImmutableMap<String, Integer> actual = ImmutableMap.of("jan", 1, "march", 3);
Expand Down

0 comments on commit bf2cc0d

Please sign in to comment.