Using Java 8.
Basically, in a unit test (junit) I have this code:
callSomeCode();
assertTrue(new File(this.getClass().getResource("/img/dest/someImage.gif").getFile()).exists());
In callSomeCode()
, I have this:
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();
The first time the test runs, this.getClass().getResource("/img/dest/someImage.gif")
returns null
although the file is well created.
The second time (when the file was already created during the first test run then just overwritten), it is non-null and the test passes.
How to make it work the first time?
Should I configure a special setup in IntelliJ to automatically refresh the folder where the file is created?
Note that I have this basic maven structure:
--src
----test
------resources