开发者

Comparing unicode characters in Junit

I had problems in some flow with unicode chars in some of my flows. So i fixed the flow and added a test.

assertEquals("Björk", buyingOption.getArtist());

the buyingOption.getArtist() will return the same name that is on ,here is a snippet :

Comparing unicode characters in Junit

but junit will fail with the message :

junit.framework.ComparisonFailure: null 
Expected :Bj?rk
Actual   :Bj?rk
    at com.delver.update.system.AECSystemTest.basicOperationtsTest1(AECSystemTest.java:40)
    at sun.reflect.NativeMethodAccessorImpl.invo开发者_开发技巧ke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)


This is probably due to the default encoding used for your Java source files. The ö in the string literal in the JUnit source code is probably being converted to something else when the test is compiled.

To avoid this, use Unicode escapes (\uxxxx) in the string literals in your JUnit source code:

assertEquals("Bj\u00F6rk", buyingOption.getArtist());


I agree with Grodriguez but would like to suggest you to change your default encoding to UTF-8 and forget about this kind of problems.

How to do this? It depends on your IDE. For example in Eclipse go to Window/Preferences then type "encoding", choose Workspace and change encoding to UTf-8


I found the solution was to change the default encoding before running mvn test

My fix to this issue was to set the ENV var JAVA_TOOL_OPTIONS before running

export JAVA_TOOL_OPTIONS="$JAVA_TOOL_OPTIONS -Dfile.encoding=UTF8"    
mvn test
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜