Recall & Review
beginner
What is the purpose of the
@ResourceLock annotation in JUnit?The
@ResourceLock annotation is used to control access to shared resources during parallel test execution, preventing conflicts by locking resources.Click to reveal answer
beginner
How do you specify the resource to lock using
@ResourceLock?You specify the resource by setting the
value attribute to a string that identifies the resource, for example, @ResourceLock("DATABASE").Click to reveal answer
intermediate
What are the two modes of locking available in
@ResourceLock?The two modes are READ and WRITE. READ allows multiple tests to access the resource concurrently, WRITE allows exclusive access.
Click to reveal answer
beginner
Why is resource locking important in parallel test execution?
Resource locking prevents tests from interfering with each other when they share resources, ensuring test reliability and avoiding flaky failures.
Click to reveal answer
intermediate
Show a simple example of using
@ResourceLock to lock a resource named "FILE" in WRITE mode.Example:<br>
@ResourceLock(value = "FILE", mode = ResourceAccessMode.WRITE)
void testMethod() {
// test code that needs exclusive access to FILE
}Click to reveal answer
What does
@ResourceLock("DB") do in a JUnit test?✗ Incorrect
@ResourceLock("DB") locks the resource named 'DB' to prevent other tests from accessing it concurrently, ensuring safe parallel execution.
Which
ResourceAccessMode allows multiple tests to read the same resource simultaneously?✗ Incorrect
The READ mode allows multiple tests to access the resource concurrently for reading.
If two tests lock the same resource in WRITE mode, what happens?
✗ Incorrect
WRITE mode locks are exclusive, so one test waits for the other to finish before running.
Which annotation is used to lock resources in JUnit 5 for parallel tests?
✗ Incorrect
The correct annotation is @ResourceLock.
Why should you use resource locking in tests that run in parallel?
✗ Incorrect
Resource locking avoids conflicts on shared resources, preventing flaky test failures.
Explain how
@ResourceLock helps manage shared resources in parallel JUnit tests.Think about how tests can interfere with each other when sharing files or databases.
You got /4 concepts.
Describe the difference between READ and WRITE modes in
@ResourceLock.Consider how reading and writing to a shared file differ.
You got /4 concepts.