2016-04-28 73 views
0

我的問題是,當資源包嘗試在JUnit測試期間獲取資源時,我得到MissingResourceException,但位於src/test/resources中的所有文件都編譯爲target/test我甚至可以使用它們來達到它們ResourceBundle無法在目標/測試類中找到資源

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

我該怎麼辦?

回答

0

'classLoader.getResource()'可能導致該問題。嘗試通過返回InputStream的ResourceAsStream獲取它。例如: -

InputStream is = this.getClass()。getClassLoader()。getResourceAsStream(「filename」);

0

我檢查了文件的代碼。我發現沒有這樣的構造函數可以構造帶有文件對象的File對象。

如果你做的maven測試確實複製資源文件到測試類。

試試這個

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


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

或只使用

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