在單元測試(junit的)我有這樣的代碼中使用Java 8.如何讀取運行時創建的文件?
基本上,:
callSomeCode();
assertTrue(new File(this.getClass().getResource("/img/dest/someImage.gif").getFile()).exists());
在callSomeCode()
,我有這樣的:
InputStream is = bodyPart.getInputStream();
File f = new File("src/test/resources/img/dest/" + bodyPart.getFileName()); //filename being someImage.gif
FileOutputStream fos = new FileOutputStream(f);
byte[] buf = new byte[40096];
int bytesRead;
while ((bytesRead = is.read(buf)) != -1)
fos.write(buf, 0, bytesRead);
fos.close();
第一次測試運行,this.getClass().getResource("/img/dest/someImage.gif")
返回null
雖然該文件是很好創建。
第二次(當第一次測試運行期間已經創建了該文件,然後剛剛被覆蓋),它是非空的,測試通過。
如何讓它第一次工作?
我應該在IntelliJ中配置特殊設置以自動刷新文件所在的文件夾?
請注意,我有這樣的基本結構行家:
--src
----test
------resources
http://stackoverflow.com/a/1011126/1587791 – nakano531