【发布时间】:2017-05-30 03:57:37
【问题描述】:
我的目录结构如下所示
Root
|--A
|--src
|--target
|--output.xml
|--B
|--src
|--test
|--java
|--MyTest.java
我的 Junit 测试方法如下所示`
@Test
public void testXmlGeneration() {
File file = new File("output.xml");
assertTrue(file.exists());
}
我想检查 output.xml 是否在目录 A 中生成。上面的测试失败了
java.lang.AssertionError
at org.junit.Assert.fail(Assert.java:86)
at org.junit.Assert.assertTrue(Assert.java:41)
我也尝试了下面的方法,但它失败了 NullPointerException。`
File file = new File(this.getClass().getClassLoader().getResourceAsStream("A/output.xml").toString());
assertTrue(file.exists());
NullPointerException 堆栈跟踪如下
java.lang.NullPointerException
at com.B.StatisticsTest.testXmlGeneration(StatisticsTest.java:37)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
【问题讨论】:
-
尝试将完整路径指定为
new File("Root/A/output.xml")并共享 NPR 堆栈跟踪。 -
它因 java.lang.AssertionError 而失败。 StackTrace 与上面相同。 @nullpointer