运行JUnit测试套件时出现致命错误

时间:2013-06-03 11:03:08

标签: java junit testcase test-suite

我正在使用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;
    }

1 个答案:

答案 0 :(得分: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中指定的编码不同。
  3. 要验证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"?>