将字符转换为 UTF-8
在我的单元测试中,我试图证明发送到客户端应用程序的 HTTP 响应仅包含有效的 UTF-8 字符。这是由于英镑符号“£”未作为 UTF-8 编码字符发送而导致客户端应用程序验证失败的问题。
在我的测试中,我执行以下操作:
// setup byte[] outputData = "£".getBytes("ISO-8859-1"); String serviceXmlResponse = String.format("<?xml version=\"1.0\" encoding=\"utf-8\" ?><some>%s</some>", outputData.toString()); String expectedXmlContent = "<?xml version=\"1.0\" encoding=\"utf-8\" ?><some>£</some>"; when(service.getMessage()).thenReturn(serviceXmlResponse); // act ModelAndView result = controller.handlePostRequest(request, response, mav); // assert assertEquals(expectedXmlContent, result.getModel().get(ApplicationController.RESPONSE_KEY));
生成的 XML 包含:
<?xml version="1.0" encoding="utf-8" ?><some>[B@1164b9b6</some>
我在这里做错了什么? 谢谢
In my unit test, i'm trying to prove that HTTP Responses sent onto client applications only contain valid UTF-8 characters. This is off the back of an issue whereby the British Pound symbol '£' was not sent as a UTF-8 encoded character, causing a validation failure for a client application.
In my test i'm doing the following:
// setup byte[] outputData = "£".getBytes("ISO-8859-1"); String serviceXmlResponse = String.format("<?xml version=\"1.0\" encoding=\"utf-8\" ?><some>%s</some>", outputData.toString()); String expectedXmlContent = "<?xml version=\"1.0\" encoding=\"utf-8\" ?><some>£</some>"; when(service.getMessage()).thenReturn(serviceXmlResponse); // act ModelAndView result = controller.handlePostRequest(request, response, mav); // assert assertEquals(expectedXmlContent, result.getModel().get(ApplicationController.RESPONSE_KEY));
The resultant XML contains:
<?xml version="1.0" encoding="utf-8" ?><some>[B@1164b9b6</some>
What am I doing wrong here?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果
service.getMessage()
将 XML 作为String
返回,而result.getModel().get(ApplicationController.RESPONSE_KEY)
返回相同的值XML 以byte[]
的形式编码,您的测试应该如下所示:否则您的测试没有多大意义,特别是如果
result.getModel().get(ApplicationController.RESPONSE_KEY )
返回一个String
,以便在生成模型属性后在视图中应用实际编码。另请参阅:
If
service.getMessage()
returns your XML as aString
, whereasresult.getModel().get(ApplicationController.RESPONSE_KEY)
returns the same XML in encoded form as abyte[]
, your test should look like this:Otherwise your test doesn't make much sense, especially if
result.getModel().get(ApplicationController.RESPONSE_KEY)
returns aString
, so that actual encoding is applied in the view, after generating model attributes.See also:
[B
是在byte
数组上调用toString()
时得到的结果。因此,您的测试不起作用,因为它基本上说:“调用
getMessage()
时返回预期输出”。因此它测试assertEquals (expected, Expected);
您的问题出在从套接字读取 XML 字节,然后将它们转换为
service.getMessage() 返回的字符串的代码中
所以你的代码必须如下所示:
也不要混合编码;如果 XML 显示“UTF-8”,则必须在
getBytes()
中使用相同的编码。[B
is what you get when you calltoString()
on abyte
array.So your test doesn't work since it basically says: "Return the expected output when
getMessage()
is called". So it testsassertEquals (expected, expected);
Your problem is in the code which reads the XML bytes from the socket and then converts them into a String that is returned by
service.getMessage()
So your code must look like:
Also don't mix encodings; if the XML says "UTF-8", then you must use the same encoding in
getBytes()
.