Testing / JUnit6 Interview Questions
How does parallel test execution work in JUnit 6?
JUnit 6 supports running tests in parallel to reduce total test suite execution time. It must be enabled via configuration and provides fine-grained control over which tests run in parallel.
# Enable parallel execution in junit-platform.properties # (src/test/resources/junit-platform.properties) # Enable parallelism globally junit.jupiter.execution.parallel.enabled=true # Strategy: dynamic (uses available processors) junit.jupiter.execution.parallel.config.strategy=dynamic junit.jupiter.execution.parallel.config.dynamic.factor=1.5 # OR: fixed thread count junit.jupiter.execution.parallel.config.strategy=fixed junit.jupiter.execution.parallel.config.fixed.parallelism=4 # Default mode: all test classes run sequentially, # all methods within a class run sequentially. # Override with: junit.jupiter.execution.parallel.mode.default=concurrent junit.jupiter.execution.parallel.mode.classes.default=concurrent
// @Execution annotation: fine-grained control per class or method import org.junit.jupiter.api.parallel.*; @Execution(ExecutionMode.CONCURRENT) // this class runs in parallel class FastParallelTest { @Test void first() { ... } @Test void second() { ... } } @Execution(ExecutionMode.SAME_THREAD) // always sequential class OrderDependentTest { @Test @Order(1) void setup() { ... } @Test @Order(2) void execute(){ ... } } // @ResourceLock: prevent concurrent access to shared resources class DatabaseTest { @Test @ResourceLock(value = "database", mode = ResourceAccessMode.READ_WRITE) void writesToDatabase() { ... } @Test @ResourceLock(value = "database", mode = ResourceAccessMode.READ) void readsFromDatabase() { ... } // Reads run concurrently with each other // Writes are exclusive (no concurrency with reads or other writes) }
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...
