如何使用XMLUnit读取两个XML文件

时间:2015-01-13 13:18:25

标签: java xml junit xmlunit

我是XML的新手。经过大量的搜索,我发现XMLUnit可以做到这一点,但问题是:     每当我将xml文件的内容更改为简单的字符串文本并尝试执行代码时,它仍然在junit测试中显示为绿色但它不应该执行虽然它抛出异常但我的要求是信号应该是红色的,即使文件是不是正确的xml格式。到目前为止我的工作。

package mypack;

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.List;

import org.custommonkey.xmlunit.DetailedDiff;
import org.custommonkey.xmlunit.Diff;
import org.custommonkey.xmlunit.XMLTestCase;
import org.custommonkey.xmlunit.XMLUnit;
import org.junit.Test;
import org.xml.sax.SAXException;

public class MyXMLTestCase extends XMLTestCase {


    enter code here
    @Test
    public void testMyXmlTestCase() {
        FileReader expected = null;
        FileReader output = null;
        try {
            expected = new FileReader("D:/vivek.xml");
            output = new FileReader("D:/rahul.xml");
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }

        XMLUnit.setNormalizeWhitespace(Boolean.TRUE);
        try {
            Diff diff = new Diff(expected,output);
            // assertTrue("XMLSimilar"+diff.toString(),diff.similar());
            //assertTrue("XMLIdentical"+diff.toString(),diff.identical());
            DetailedDiff mydiff = new DetailedDiff(diff);
            List allDifferences = mydiff.getAllDifferences();
            assertEquals(mydiff.toString(), 0, allDifferences.size());

        } catch (SAXException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}

0 个答案:

没有答案