Testing / JUnit6 Interview Questions
How does parallel test execution work in JUnit 6?
JUnit 6 supports parallel test execution at both the class and method level. It is disabled by default and is configured via junit-platform.properties or programmatic launcher configuration. The implementation uses Java's ForkJoinPool.
# junit-platform.properties (in src/test/resources/) # Enable parallel execution junit.jupiter.execution.parallel.enabled=true # Strategy: DYNAMIC (based on CPU count) or FIXED (explicit thread count) junit.jupiter.execution.parallel.config.strategy=dynamic junit.jupiter.execution.parallel.config.dynamic.factor=2 # thread count = processor count * factor (e.g. 4 cores * 2 = 8 threads) # OR: fixed thread count # junit.jupiter.execution.parallel.config.strategy=fixed # junit.jupiter.execution.parallel.config.fixed.parallelism=4 # Default execution mode: SAME_THREAD (sequential) or CONCURRENT junit.jupiter.execution.parallel.mode.default=concurrent junit.jupiter.execution.parallel.mode.classes.default=concurrent
// Fine-grained control with @Execution: import org.junit.jupiter.api.parallel.*; @Execution(ExecutionMode.CONCURRENT) // This class runs in parallel class ParallelTests { @Test void test1() { /* can run in parallel with test2 */ } @Test void test2() { /* can run in parallel with test1 */ } } @Execution(ExecutionMode.SAME_THREAD) // This class runs sequentially class SequentialTests { // Useful for tests with shared mutable state or DB transactions } // @ResourceLock: prevent concurrent access to named resources @ResourceLock(value = "database", mode = ResourceAccessMode.READ_WRITE) void dbWriteTest() { /* exclusive DB access */ } @ResourceLock(value = "database", mode = ResourceAccessMode.READ) void dbReadTest() { /* shared read access -- can run with other READ tests */ }
Invest now in Acorns!!! 🚀
Join Acorns and get your $5 bonus!
Acorns is a micro-investing app that automatically invests your "spare change" from daily purchases into diversified, expert-built portfolios of ETFs. It is designed for beginners, allowing you to start investing with as little as $5. The service automates saving and investing. Disclosure: I may receive a referral bonus.
Invest now!!! Get Free equity stock (US, UK only)!
Use Robinhood app to invest in stocks. It is safe and secure. Use the Referral link to claim your free stock when you sign up!.
The Robinhood app makes it easy to trade stocks, crypto and more.
Webull! Receive free stock by signing up using the link: Webull signup.
More Related questions...
