简体   繁体   中英

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 :

替代文字

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.invoke0(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 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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM