From version 8.1
edited by Vincent Massol
on 2018/11/27 15:47
To version 9.1
edited by Vincent Massol
on 2019/08/02 09:37
Change comment: There is no comment for this version

Summary

Details

Page properties
Content
... ... @@ -340,6 +340,31 @@
340 340  {{/code}}
341 341  )))
342 342  
343 +== Temporary Directory ==
344 +
345 +From time to time, tests require a temporary directory. We must not use the system's temporary directory nor use JUnit5's ##@TempDir## annotation (it also uses the system's temporary directory) for the following reasons:
346 +* Leftover data in a shared location once the test has finished
347 +* Creates a state that can make other tests fail
348 +* Generate errors in Jenkins since Jenkins monitors created files and doesn't allow to remove files outside of the Worskspace
349 +
350 +Thus the best practice is to use the ##XWikiTempDir## annotation as in the following examples:
351 +
352 +{{code language="java"}}
353 [email protected]
354 +private static File TEST_DIR;
355 +
356 [email protected]
357 +private File tmpDir;
358 +
359 [email protected]
360 +public void testXXX(@TempDir File tmpDir)
361 +{
362 + ...
363 +}
364 +{{/code}}
365 +
366 +The ##XWikiTempDir## annotation will create a unique temporary directory inside Maven's ##target## directory.
367 +
343 343  = Examples =
344 344  
345 345  * [[Examples of ##@ComponentTest## in xwiki-commons>>https://github.com/xwiki/xwiki-commons/search?utf8=%E2%9C%93&q=%22%40ComponentTest%22&type=]]

Get Connected