When running Junit tests using gradle with custom testFunctional source set with maxParallelForks=1 my base class initialization code is invoked multiple times by different workers executing the custom testFunctional task (seems to me that gradle worker isolationLevel=process is used by Junit) from multiple gradle modules.
I'd like to be able to control the parallelism of test execution using Junit5's mechanisms, but I also need to ensure synchronized initialization of some of the resources in the shared abstract class of my functional tests.
I've created this gist to better reflect the issue: https://gist.github.com/s-kramer/a5d356118a74b8a2fb75003d0dc037ce
Considered solutions
Ideally I'd like to avoid a file-based locking system as this just seems messy.
I'd also like to keep --parallel option to not degrade the build time and enable running tasks in parallel.
Using --max-workers limits active workers, but I'd like to still enable parallel test execution after the shared initialization code is finished.
I was also thinking about moving the initialization code to a separate gradle tasks, but I'm worried that this will separate the setup from the test code too much + I'm not sure how this would have to look like.