Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 :

alt text

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)
like image 667
Roman Avatar asked Sep 18 '25 20:09

Roman


2 Answers

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());
like image 196
Grodriguez Avatar answered Sep 20 '25 09:09

Grodriguez


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

like image 22
AlexR Avatar answered Sep 20 '25 10:09

AlexR