Unit test being confused by three question marks

310 views Asked by At

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.

1

There are 1 answers

0
Chris Willmore On

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.