我正在使用將字符串轉換爲其等效Unicode字符的庫。問題是,在編寫單元測試以將Unicode值與自身進行比較時,即使兩個值相等,測試也會失敗。無法測試將字符串轉換爲Unicode
這裏是我的代碼:
@Test
public void whenTranslate_thenCorrect() {
UnicodeEscaper ue = UnicodeEscaper.above(0);
String result = ue.translate("ABCD");
Assert.assertEquals("\u0041\u0042\u0043\u0044", result);
}
即使result
對象的值保持相同的值預期,測試失敗。這裏是我得到的跟蹤:
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)
爲什麼它期望在翻譯前有相同的值?
' 「\ u0041 \ u0042 \ u0043 \ u0044」'在源代碼表示' 「ABCD」'在運行時該程序,這是不等於「'\ u0041 \ u0042 \ u0043 \ u0044" '。如果你想在運行時使用'\ u0041 \ u0042 \ u0043 \ u0044'的值,那麼在源代碼字符串文字''\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\''''中跳過反斜槓。 –
工作正常。謝謝! –