比较 Junit 中的 unicode 字符
我在某些流程中使用 unicode 字符时遇到了问题。所以我修复了流程并添加了测试。
assertEquals("Björk", buyingOption.getArtist());
buyOption.getArtist() 将返回与上相同的名称,这是一个片段:
但 junit 将失败消息:
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)
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)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这可能是由于 Java 源文件使用的默认编码造成的。在编译测试时,JUnit 源代码中字符串文字中的
ö
可能会被转换为其他内容。为了避免这种情况,请在 JUnit 源代码的字符串文字中使用 Unicode 转义符 (
\uxxxx
):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:我同意 Grodriguez 的观点,但建议您将默认编码更改为 UTF-8,并忘记此类问题。
如何做到这一点?这取决于您的 IDE。例如,在 Eclipse 中,转到 Window/Preferences,然后输入“encoding”,选择 Workspace 并将编码更改为 UTf-8
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
我发现解决方案是在运行 mvn test 之前更改默认编码
我对此问题的修复是在运行之前设置 ENV var JAVA_TOOL_OPTIONS
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