Search code examples
javajunittestcasetest-suite

Fatal Error when running a JUnit test suite


I'm using a JUnit test suite in order run a bunch of JUnit test cases, and i got no errors and no failures and all the tests run correctly, but i have the following message displayed in the console and i don't know what's the meaning of it:

[Fatal Error] :1:1: Premature end of file.

when i use the error handler i got the following result:

caught exception(fatal error)
org.xml.sax.SAXParseException; lineNumber: 1; columnNumber: 1; Premature end of file.
    at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.createSAXParseException(Unknown Source)
    at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.fatalError(Unknown Source)
    at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(Unknown Source)
    at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(Unknown Source)
    at com.sun.org.apache.xerces.internal.impl.XMLScanner.reportFatalError(Unknown Source)
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl$PrologDriver.next(Unknown Source)
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(Unknown Source)
    at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.next(Unknown Source)
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(Unknown Source)
    at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(Unknown Source)
    at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(Unknown Source)
    at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(Unknown Source)
    at com.sun.org.apache.xerces.internal.parsers.DOMParser.parse(Unknown Source)
    at com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderImpl.parse(Unknown Source)
    at javax.xml.parsers.DocumentBuilder.parse(Unknown Source)
    at com.nc.inotify.dp.xml.impl.XmlSource.convert(XmlSource.java:328)
    at com.nc.inotify.dp.xml.impl.XmlSource.update(XmlSource.java:299)
    at com.nc.inotify.dp.xml.junit.TXmlSource.testUpdate(TXmlSource.java:106)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:45)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:42)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:263)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:68)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:47)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222)
    at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
    at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:30)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:300)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
    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)

and that's the method that contains the error:

private Document convert(String xml) throws ParserConfigurationException,
            SAXException, IOException {

        // convert String into InputStream
        InputStream is = new ByteArrayInputStream(xml.getBytes());

        // convert InputStream to Document
        domFactory = DocumentBuilderFactory.newInstance();
        domFactory.setNamespaceAware(true);
        builder = domFactory.newDocumentBuilder();
        builder(builder);
        is.reset();
        Document doc = builder.parse(is);
        is.close();

        return doc;
    }

Solution

  • This looks like an error from your sax parser (or whichever XML parser you're using). To locate the error, try adding an ErrorHandler to xml handling. For instance, for a DocumentBuilder, you can can call setErrorHandler():

    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder = dbf.newDocumentBuilder();
    builder.setErrorHandler(new ErrorHandler() {
    
      @Override
      public void warning(SAXParseException exception) throws SAXException {
        System.err.println("warning: caught exception");
        exception.printStackTrace(System.err);
      }
    
      @Override
      public void fatalError(SAXParseException exception) throws SAXException {
        System.err.println("fatalError: caught exception");
        exception.printStackTrace(System.err);
      }
    
      @Override
      public void error(SAXParseException exception) throws SAXException {
        System.err.println("error: caught exception");
        exception.printStackTrace(System.err);
      }
    });
    

    This will give you a better clue about what is happening.

    For more information, look at com.sun.org.apache.xerces.internal.util.DefaultErrorHandler#fatalError, which is I believe what is creating this error message.

    EDIT: With the stacktrace, and the extra code, it looks like either:

    1. xml is empty
    2. xml has a different encoding from the one you've specified in the XML.

    To verify 2, does your XML contains non-ascii characters, or does the XML have a correct declaration with the correct encoding, for instance:

    <?xml version="1.0" encoding="us-ascii"?>
    <?xml version="1.0" encoding="windows-1252"?>
    <?xml version="1.0" encoding="ISO-8859-1"?>
    <?xml version="1.0" encoding="UTF-8"?>
    <?xml version="1.0" encoding="UTF-16"?>