Re: Write to disk in Platform based JUnit tests
Tim Boudreau <[email protected]>
| Newsgroups | gmane.comp.java.netbeans.modules.openide.devel |
|---|---|
| Message-ID | <CA+qecROFktgV_Q_T6=xjyk9_7uKRsDjwENasW9HC-v34q4gDVw@mail.gmail.com> |
Most tests don't need the system filesystem to be files on disk. Having a
default for tests that's in-memory means that:
- You don't fill up the user's temp directory with test files (on a
Jenkins server you can eventually knock it down that way, and it's not nice
to do to anyone)
- You never have to worry about test files from a previous build
interfering with tests run on the next one - unless you remember to
*always* incorporate something like System.currentTimeMillis() into your
test folder name, that's easy to do and easy to go a long time before you
realize your test is passing, not because your code works but because it's
running against stale data
Both of those things are common enough problems that you don't want to have
them if you can avoid it.
If whatever code you're calling that needs a file can possibly take an
InputStream instead, that would be the quickest solution. But if not,
there are ways to have an on-disk system filesystem for a test (probably a
bit of setup ugliness to do it). Just remember the caveats above - use
some incantation like
File tmp = new File(System.getProperty("java.io.tmpdir"));
File testFolder = new File(tmp, "mytest-" +
Long.toString(System.currentTimeMillis(), 36));
assertTrue(testFolder.mkdirs());
// use that as your system fs root - base 36 just gets you a shorter ascii
form of the timestamp
and in your teardown, actually delete that folder and its subtree.
-Tim
On Fri, Dec 4, 2015 at 7:03 AM, mhaslinger <[email protected]> wrote:
> OK I still do not get it why NB Platform uses different "locations"
> regarding the users Directory (with Windows2Local etc) for Runtime and
> Testing and how to change this. I could not find any explanation in the doc
> or code yet.
>
>
>
>
>
--
http://timboudreau.com