0

我正在使用 JUnit 测试套件来运行一堆 JUnit 测试用例,我没有错误也没有失败,并且所有测试都运行正确,但是我在控制台中显示了以下消息,我不知道是什么它的含义:

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

当我使用错误处理程序时,我得到以下结果:

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)

这就是包含错误的方法:

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;
    }
4

1 回答 1

2

这看起来像是来自您的 sax 解析器(或您正在使用的任何 XML 解析器)的错误。要定位错误,请尝试将 ErrorHandler 添加到 xml 处理。例如,对于 DocumentBuilder,您可以调用 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);
  }
});

这将使您更好地了解正在发生的事情。

有关更多信息,请查看com.sun.org.apache.xerces.internal.util.DefaultErrorHandler#fatalError,我相信这是创建此错误消息的原因。

编辑:使用堆栈跟踪和额外的代码,它看起来像:

  1. xml是空的
  2. xml与您在 XML 中指定的编码不同。

要验证 2,您的 XML 是否包含非 ascii 字符,或者 XML 是否具有具有正确编码的正确声明,例如:

<?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"?>
于 2013-06-04T12:22:07.530 回答