JUnit运行器从java.io.Writer获取NullPointerException

时间:2010-11-19 16:38:51

标签: java junit surefire


我从Maven Surefire插件获得NullPointerException。它仅在使用DBUnit的测试中发生。 Surefire报告文件为空。

-------------------------------------------------------
 T E S T S
-------------------------------------------------------
org.apache.maven.surefire.booter.SurefireExecutionException: null; nested exception is java.lang.NullPointerException: null
java.lang.NullPointerException
    at java.io.Writer.write(Writer.java:140)
    at java.io.PrintWriter.newLine(PrintWriter.java:436)
    at java.io.PrintWriter.println(PrintWriter.java:585)
    at java.io.PrintWriter.println(PrintWriter.java:696)
    at org.apache.maven.surefire.report.AbstractFileReporter.testSetStarting(AbstractFileReporter.java:59)
    at org.apache.maven.surefire.report.ReporterManager.testSetStarting(ReporterManager.java:219)
    at org.apache.maven.surefire.suite.AbstractDirectoryTestSuite.executeTestSet(AbstractDirectoryTestSuite.java:138)
    at org.apache.maven.surefire.suite.AbstractDirectoryTestSuite.execute(AbstractDirectoryTestSuite.java:127)
    at org.apache.maven.surefire.Surefire.run(Surefire.java:177)
    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 org.apache.maven.surefire.booter.SurefireBooter.runSuitesInProcess(SurefireBooter.java:338)
    at org.apache.maven.surefire.booter.SurefireBooter.main(SurefireBooter.java:997)
[INFO] ------------------------------------------------------------------------
[ERROR] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] There are test failures.

当我尝试使用JUnit运行程序从NetBeans IDE运行相同的测试时,我得到了相同的异常:

Exception in thread "main" java.lang.NullPointerException
        at java.io.Writer.write(Writer.java:140)
        at org.apache.tools.ant.util.DOMElementWriter.write(DOMElementWriter.java:212)
        at org.apache.tools.ant.taskdefs.optional.junit.XMLJUnitResultFormatter.endTestSuite(XMLJUnitResultFormatter.java:171)
        at org.apache.tools.ant.taskdefs.optional.junit.JUnitTestRunner.fireEndTestSuite(JUnitTestRunner.java:714)
        at org.apache.tools.ant.taskdefs.optional.junit.JUnitTestRunner.run(JUnitTestRunner.java:547)
        at org.apache.tools.ant.taskdefs.optional.junit.JUnitTestRunner.launch(JUnitTestRunner.java:1031)
        at org.apache.tools.ant.taskdefs.optional.junit.JUnitTestRunner.main(JUnitTestRunner.java:888)
Test football.dao.jpa.SimpleJPATest FAILED (crashed)

java.io.Writer类中的 write 方法只有一个String参数。这意味着跑步者必须将null作为参数传递。但是这是怎么发生的呢?

public void write(String str) throws IOException {
    write(str, 0, str.length());
}

1 个答案:

答案 0 :(得分:5)

您实际看到的是line.separator系统属性为null。我无法解释为什么根据我所看到的,但如果你可以测试它尝试做:

java.security.AccessController.doPrivileged(
               new sun.security.action.GetPropertyAction("line.separator"));