简体   繁体   English

ResourceBundle在目标/测试类中找不到资源

[英]ResourceBundle can't find resources in target/test-classes

My problem is that when resource bundle tries to get resources during JUnit test I get MissingResourceException, but all files that located in src/test/resources are compiled into target/test-classes and I even can reach them using 我的问题是,当资源捆绑在JUnit测试期间尝试获取资源时,我得到MissingResourceException,但是位于src / test / resources中的所有文件都被编译为目标/测试类,我什至可以使用

 ClassLoader classLoader = getClass().getClassLoader();
        File file = new File(classLoader.getResource("someFile.properties").getFile());
        System.out.println(file.getAbsolutePath()); 

What should I do? 我该怎么办?

'classLoader.getResource()' may be causing the issue. “ classLoader.getResource()”可能是导致此问题的原因。 Try getting it through ResourceAsStream which returns InputStream. 尝试通过返回InputStream的ResourceAsStream进行获取。 for example:- 例如:-

InputStream is = this.getClass().getClassLoader().getResourceAsStream("filename"); InputStream是= this.getClass()。getClassLoader()。getResourceAsStream(“ filename”);

I checked File's code. 我检查了文件的代码。 I found there is no such constructor that construct a File object with a file object. 我发现没有使用文件对象构造File对象的构造函数。

If you do maven test does copy resource file to test-classes. 如果执行了maven测试,则确实会将资源文件复制到测试类。

try this 尝试这个

// this will show your resoure root.
File file = new File(getClass().getResource("/").getPath());


// debug you may find your file here.
File[] resoureces = file.listFiles();

or just use 或只是使用

File file = new File(getClass().getResource("/").getPath() + "someFile.properties");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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