2

Considering the following example:

assertEquals( "I am expecting this value on one line.\r\nAnd this value on this line",
    "I am expecting this value on one line.\nAnd this value on this line" );

Are there any tweaks or plugins to\in Eclipse that could help to identify the extra '\r' (or for that matter other non-printable) characters in a String comparison?

The current result comparison does not really help me to identify the problem: extra carriage return result comparison

javamonkey79
  • 17,443
  • 36
  • 114
  • 172

3 Answers3

2

For cases where the assertion must be sensitive to 'non printable characters', you may use a custom assertion method which converts non printable characters to their unicode representation before comparison. Here is a some quickly written code for illustration (inspired by this and this):

package org.gbouallet;

import java.awt.event.KeyEvent;

import org.junit.Assert;
import org.junit.Test;

public class NonPrintableEqualsTest {

@Test
public void test() {
    assertNonPrintableEquals(
            "I am expecting this value on one line.\r\nAnd this value on this line",
            "I am expecting this value on one line.\nAnd this value on this line");
}

private void assertNonPrintableEquals(String string1,
        String string2) {
    Assert.assertEquals(replaceNonPrintable(string1),
            replaceNonPrintable(string2));

}

public String replaceNonPrintable(String text) {
    StringBuffer buffer = new StringBuffer(text.length());
    for (int i = 0; i < text.length(); i++) {
        char c = text.charAt(i);
        if (isPrintableChar(c)) {
            buffer.append(c);
        } else {
            buffer.append(String.format("\\u%04x", (int) c));
        }
    }
    return buffer.toString();
}

public boolean isPrintableChar(char c) {
    Character.UnicodeBlock block = Character.UnicodeBlock.of(c);
    return (!Character.isISOControl(c)) && c != KeyEvent.CHAR_UNDEFINED
            && block != null && block != Character.UnicodeBlock.SPECIALS;
}
}
Community
  • 1
  • 1
Guy Bouallet
  • 2,099
  • 11
  • 16
  • This is a little bit of a hack, but still not a bad idea. It seems a bit funny to change the unit test data itself, though, I understand why. Ideally, something like this would be integrated in to the junit plugin(s). +1 – javamonkey79 Jan 29 '14 at 00:50
1

You could write your own assertion method (not using any of those from the Assert class) that throws a junit.framework.ComparisonFailure.ComparisonFailure with the expected and actual values converted in a way that displays the non-printable characters (like the replaceNonPrintable(String) method in the answer by @GuyBouallet). That custom assertion can't use Assert.assertEquals() because it throws the exception with the original objects (Strings, in your case) as the paramters; you need to throw the exception with modified versions of the input.

E-Riz
  • 31,431
  • 9
  • 97
  • 134
0

first of all check other testing frameworks like assertj and hamcrest matchers. they have much better reporting part - they may have this feature out of the box.

if not, then: if you expect to have this issue in this only single test then do as @Guy Bouallet said - write your own assertion. but if your application does a lot of this kind string comparison than instead of writing many different asertions (equals/substring/matches etc) just use string normalization. before you pass the string to the assert method, replace all the white characters to something else

piotrek
  • 13,982
  • 13
  • 79
  • 165