Junit + getResourceAsStream返回Null [英] Junit + getResourceAsStream Returning Null
问题描述
不确定这是怎么回事。我重读了getResourceAsStream并且它总是返回null。
Not sure how this is possible. I re-read up on getResourceAsStream and it's always returning null.
InputStream source = this.getClass().getResourceAsStream("test.xml");
Finder中的test.java旁边(使用OS X和Eclipse)是test.xml
Right next to test.java in the Finder (using OS X and Eclipse) is test.xml
我可以在TextWrangler中打开它并将其视为现有的数据。
I can open it in TextWrangler and view it as existing with data inside.
这是一个Junit测试,如果它有所不同。我去查看我们系统上现有的Junit测试,我使用的方式与工作示例完全相同(如文件所在位置和代码本身)。
This is a Junit test if it makes any difference. I went and looked at existing Junit tests on our system and I'm using it in the exactly same manner as a working example (as in where the file is located and the code itself).
可以防止我假设getClass()返回正确的路径会有什么小的差别?
What small difference could there be preventing I assume getClass() from returning the right path?
谢谢!
推荐答案
getResourceAsStream()
正在使用CLASSPATH,因此它将从加载,无论你的类在哪里,而不是您的源文件。
getResourceAsStream()
is using the CLASSPATH, and as such it will load from wherever your classes are, not your source files.
我怀疑您需要将XML复制到与.class文件相同的目录。
I suspect you need to copy your XML to the same directory as your .class file.
这篇关于Junit + getResourceAsStream返回Null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!