Search code examples
javaunit-testingjunitxmlunit

How to catch AssertPathsEqual and print error to console?


How could I catch this AssertionFailedError and print to the console an message that says:

"Expected text value '55555' but was '55556' at /xpathResult[1]/result[2]/field[1]/field[1]/field[6]/text()[1] for [testFileName] and [testName]"

I'm not exactly sure how to catch the values out from the assertion without having to "hack" and manually parse the diff output string. I would rather have an elegant method rather than a "hack" method.

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)

Am I going to have to write my own DiffDescriber class that parses the Diff text output?


Solution

  • You can catch the AssertionError to get the detailed error message to the console.

    You can try something like this :

           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());
            }
    

    But I think rather than catching AssertionError it is better to get more details about
    assertion failure by making use of the class "Diff" (from org.custommonkey.xmlunit).
    You can use this class to check if XML documents being compared are similar
    (or identical) and to get more details about the difference. You can also use
    the DetailedDiff class to get a detailed diff.

    Check Example4 and Example5 from the XMLUnit documentation. These examples show the usage of Diff and DetailedDiff.