Changes for page Java Unit Testing

Last modified by Vincent Massol on 2023/05/03 15:57

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

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 +@XWikiTempDir
354 +private static File TEST_DIR;
355 +
356 +@XWikiTempDir
357 +private File tmpDir;
358 +
359 +@Test
360 +public void testXXX(@XWikiTempDir 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