如何捕获AssertPathsEqual并将错误打印到控制台?

时间:2009-12-07 17:15:28

标签: java unit-testing junit xmlunit

我怎样才能捕获此AssertionFailedError并在控制台上打印一条消息:

“预期文本值'55555'但在/ xpathResult [1] / result [2] / field [1] / field [1] / field [6] / text()[1]中为'55556' testFileName]和[testName]“

我不确定如何从断言中捕获值而不必“破解”并手动解析diff输出字符串。我宁愿使用优雅的方法而不是“黑客”方法。

junit.framework.AssertionFailedError: org.custommonkey.xmlunit.Diff
[different] Expected text value '55555' but was '55556' - comparing <field ...>55555</field> at /xpathResult[1]/result[2]/field[1]/field[1]/field[6]/text()[1] to <field ...>55556</field> at /xpathResult[1]/result[2]/field[1]/field[1]/field[6]/text()[1]

 at junit.framework.Assert.fail(Assert.java:47)
 at org.custommonkey.xmlunit.XMLAssert.assertXMLEqual(XMLAssert.java:125)
 at org.custommonkey.xmlunit.XMLAssert.assertXMLEqual(XMLAssert.java:113)
 at org.custommonkey.xmlunit.XMLAssert.assertXpathEquality(XMLAssert.java:582)
 at org.custommonkey.xmlunit.XMLAssert.assertXpathsEqual(XMLAssert.java:453)
 at org.custommonkey.xmlunit.XMLAssert.assertXpathsEqual(XMLAssert.java:435)
 at org.custommonkey.xmlunit.XMLTestCase.assertXpathsEqual(XMLTestCase.java:454)
 at tst.PatrolTests.compareXMLEqualityToLastTest(PatrolTests.java:353)
 at tst.PatrolTests.doPlate(PatrolTests.java:141)
 at tst.PatrolTests.testPlate(PatrolTests.java:117)
 at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
 at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
 at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
 at java.lang.reflect.Method.invoke(Method.java:597)
 at junit.framework.TestCase.runTest(TestCase.java:168)
 at junit.framework.TestCase.runBare(TestCase.java:134)
 at junit.framework.TestResult$1.protect(TestResult.java:110)
 at junit.framework.TestResult.runProtected(TestResult.java:128)
 at junit.framework.TestResult.run(TestResult.java:113)
 at junit.framework.TestCase.run(TestCase.java:124)
 at junit.framework.TestSuite.runTest(TestSuite.java:232)
 at junit.framework.TestSuite.run(TestSuite.java:227)
 at org.junit.internal.runners.JUnit38ClassRunner.run(JUnit38ClassRunner.java:79)
 at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:46)
 at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
 at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
 at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
 at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
 at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)

我是否必须编写自己的DiffDescriber类来解析Diff文本输出?

1 个答案:

答案 0 :(得分:1)

您可以捕获AssertionError以获取到控制台的详细错误消息。

您可以尝试这样的事情:

       try{
           // say you are comparing FileReader objects that
           // refer to XML documents
           assertXMLEqual(filereader1, filereader2); 
        } catch (final SAXException e) {
           // show faiulre message
        } catch (final IOException e) {
           // show failure message
        } catch (final AssertionError e) {
            System.err.println(e.toString());
        }

但我认为不是捕获AssertionError而是获取有关
的更多细节 通过使用“Diff”类(来自org.custommonkey.xmlunit)断言失败。
您可以使用此类检查所比较的XML文档是否相似 (或相同)并获得有关差异的更多细节。你也可以使用
详细的diffDiff类得到详细的差异。

从中检查Example4和Example5 XMLUnit文档。这些示例显示了Diff和DetailedDiff的用法。