xml字符串比较的字符串比较失败

时间:2013-07-18 13:15:24

标签: java

我正在进行Junit测试,其中方法的输出是XML格式的String。我不确定为什么字符串比较失败

Assert.assertEquals("<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>\n" +
            "<RATE>1.0</RATE>", XMLUtil.formatXML(TEST_XML));


Actaul response from the method :
 <?xml version="1.0" encoding="ISO-8859-1"?>
<RATE>1.0</RATE>

错误:

junit.framework.ComparisonFailure: expected:<...oding="ISO-8859-1"?>[
<RATE>1.0</RATE>]> but was:<...oding="ISO-8859-1"?>[
<RATE>1.0</RATE>
]>
    ]

修改 即使最后添加新行也没有帮助:

Assert.assertEquals("<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>\n" +
                "<RATE>1.0</RATE>\n", XMLUtil.formatXML(TEST_XML));

junit.framework.ComparisonFailure: expected:<...oding="ISO-8859-1"?>[
<RATE>1.0</RATE>]
> but was:<...oding="ISO-8859-1"?>[
<RATE>1.0</RATE>
]
>

1 个答案:

答案 0 :(得分:1)

预期的和实际的XML可能具有不同的托架控制字符内容。尝试删除所有换行符和换行符

String expected = 
       "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>RATE>1.0</RATE>";
Assert.assertEquals(expected,
       "<RATE>1.0</RATE>", XMLUtil.formatXML(TEST_XML).replaceAll("[\r\n]", "");