1

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

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>
]
>
4

1 回答 1

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]", "");
于 2013-07-18T13:24:36.347 回答