Hi everyone, i have many test classes, each has @BeforeAll which have .clean() of the same DB, and then creating some user and templates etc. and when im starting many classes at the same time they are deleting these users from previous one. They need to run parallel, do you have the best way to solve this? (We don't use spring)
#Problem with cleaning DB in parallel testing.
6 messages · Page 1 of 1 (latest)
<@&987246399047479336> please have a look, thanks.
Here is an AI assisted attempt to answer your question 🤖. Maybe it helps! In any case, a human is on the way 👍. To continue talking to the AI, you can use </chatgpt:1108714622413963314>.
Hi everyone, i have many test classes, each has @BeforeAll which have .clean() of the same DB, and then creating some user and templates etc. and when im starting many classes at the same time they are deleting these users from previous one. They need to r
The core issue here stems from parallel test execution where multiple classes run @BeforeAll simultaneously, each invoking .clean() on the same DB. This causes race conditions and interference, leading to unpredictable test setup states.
Key Points:
@BeforeAllruns once per test class, so concurrent executions mean multiple clean operations clash.- The
.clean()method is not thread-safe with shared DB resources. - Parallel testing requires careful DB state management to avoid conflicts.
Suggestions without rewriting code:
- Avoid running classes with shared DB state fully in parallel; group tests by isolation requirements.
- Use database transactions with rollback for each test or class, ensuring clean state without explicit clean calls.
- Employ test containers or embedded databases to give each test class an isolated environment.
- Synchronize or serialize
.clean()calls manually if parallelism is necessary but DB is shared. - Consider leveraging JUnit 5’s
@TestInstance(Lifecycle.PER_CLASS)combined with synchronized setup or mutex lock.
This approach helps maintain test independence without codebase overhaul.
Useful links:
- AI generated response using gpt-4.1-mini model
parallel sqlite db
we use oracle db
parallel schema?