java - How to read a file created at runtime? -
using java 8.
basically, in unit test (junit) have code:
callsomecode(); asserttrue(new file(this.getclass().getresource("/img/dest/someimage.gif").getfile()).exists());
in callsomecode()
, have this:
inputstream = 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 test runs, this.getclass().getresource("/img/dest/someimage.gif")
returns null
although file created.
the second time (when file created during first test run overwritten), non-null , test passes.
how make work first time?
should configure special setup in intellij automatically refresh folder file created?
note have basic maven structure:
--src ----test ------resources
as comment nakano531 points out - problem not file system, classpath. you're trying read file using classloader invoking getclass().getresource(...)
methods rather reading file using classes access file system directly.
for example, if had written test this:
callsomecode(); file file = new file("src/test/resources/img/dest/someimage.gif"); asserttrue(file.exists());
you wouldn't have had issue you're having now.
your other option implement solution link nakano531 provided: https://stackoverflow.com/a/1011126/1587791
Comments
Post a Comment