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;
}
}