Ahmed Tawila Ahmed Tawila - 3 months ago 14
Java Question

Cannot test converting Strings to Unicode

I am using a library that translates Strings to their equivalent Unicode characters. The problem is, when writing a unit test to compare the Unicode value to itself, the test fails, even though both values are equal.

Here's my code:

@Test
public void whenTranslate_thenCorrect() {
UnicodeEscaper ue = UnicodeEscaper.above(0);
String result = ue.translate("ABCD");

Assert.assertEquals("\u0041\u0042\u0043\u0044", result);
}


Even though the value of the
result object
holds the same value as the expected, the test fails. Here is the trace I am getting:

org.junit.ComparisonFailure: expected:<[ABCD]> but was:<[\u0041\u0042\u0043\u0044]>
at org.junit.Assert.assertEquals(Assert.java:115)
at org.junit.Assert.assertEquals(Assert.java:144)
at com.play.text.UnicodeEscaperTest.whenTranslate_thenCorrect(UnicodeEscaperTest.java:14)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)


Why is it expecting the same value before the translation?

Answer Source

In your source file, you've written

Assert.assertEquals("\u0041\u0042\u0043\u0044", result);

The snippets like \u0041 are Unicode escapes. These are recognized and translated to corresponding UTF-16 code units before the compiler looks at the code. In other words, that line of code above, is first actually translated to

Assert.assertEquals("ABCD", result);

as part of the compilation. This means that at runtime, you're comparing the value of result with the String value "ABCD". But your result variable is actually referencing a String with the value "\u0041\u0042\u0043\u0044", literally, ie. constructed at runtime, not as Unicode escapes in source code.

If you want that value as a String literal in your source code, you have to escape the \ so that the compiler doesn't interpret what follows as a Unicode escape.

For example

Assert.assertEquals("\\u0041\\u0042\\u0043\\u0044", result);