-1

Im writing some junits, and have this check, comparing the keys and values of two hashmaps

Iterator<Map.Entry<String, String>> it = expected.entrySet().iterator();
while (it.hasNext()) {
    Map.Entry<String, String> pairs = (Map.Entry<String, String>) it.next();
    assertTrue("Checks key exists", actual.containsKey(pairs.getKey()));
    assertThat("Checks value", actual.get(pairs.getKey()), equalTo(pairs.getValue()));
}

Works great, but i have a value that trips it up:

java.lang.AssertionError: Checks value
Expected: "Member???s "
     but: was "Member���s "
    at org.hamcrest.MatcherAssert.assertThat(MatcherAssert.java:20)

I checked the data, and the data is correct. it appears that the triple ? are tripping up something somehow. Does anyone know why this would be tripped? It seems pretty basic to me, its not even hamcrest getting messed up, its the actual assert.

scphantm
  • 4,293
  • 8
  • 43
  • 77
  • Where is the data coming from? From a file? Check the encoding. – nrainer Nov 19 '14 at 19:02
  • Its coming from an oracle database. The fact that its mapped to a string should equalize them shouldn't it? – scphantm Nov 19 '14 at 19:44
  • 5
    Print the loaded value out to the console or write it to the log. There must be an encoding issue. I don't think that JUnit/Hamcrest is the problem. – nrainer Nov 19 '14 at 20:02
  • 1
    Side note: you don't need the cast in `Map.Entry pairs = (Map.Entry) it.next();`. – eee Nov 20 '14 at 07:15
  • Those look like non-printable characters rather than question marks. Try printing their Unicode values. – David Harkness Nov 21 '14 at 03:36

1 Answers1

0

You have an encoding conflict. Many different ways this could manifest itself, but generally caused by not enforcing consistent encoding across the board.

Assuming you are using UTF-8 somewhere...

  • If using Maven, set property project.build.sourceEncoding to UTF-8 See doc for more details. Other build systems will certainly have options to specify code and resource file encodings.
  • If using IO to read (or write), always specify the encoding. For example, when reading from a file: InputStream is = new FileInputStream(file), "UTF8");

In short, find any build system setting for encoding, and any point where IO is involved when reading text, and ensure your desired encoding is set.

Chris Willmore
  • 574
  • 3
  • 13