在Junit中比较unicode字符

在Junit中比较unicode字符

本文介绍了在Junit中比较unicode字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的某些流程中的Unicode字符在某些流程中存在问题.所以我修正了流程并添加了测试.

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());

purchaseOption.getArtist()将返回与上相同的名称,这是一个代码段:

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

但是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.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)

推荐答案

这可能是由于Java源文件使用了默认编码.编译测试时,JUnit源代码中的字符串文字中的ö可能正在转换为其他内容.

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.

为避免这种情况,请在JUnit源代码的字符串文字中使用Unicode转义(\uxxxx):

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

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

这篇关于在Junit中比较unicode字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-15 09:40