简体   繁体   中英

JUnit runner gets NullPointerException from java.io.Writer


I'm getting a NullPointerException from Maven Surefire plugin. It occurs only on a test that is using DBUnit. The Surefire report file is empty.

-------------------------------------------------------
 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.

When I try to run the same test from NetBeans IDE using a JUnit runner, I get the same exception:

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)

The write method from java.io.Writer class has only one String parameter. That means the runner must be passing null as an argument. But how does this happen?

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

What youre actually seeing is the line.separator System's property is null. I cannot explain why based on what I see but if you can test it try doing:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM