0
0
JUnittesting~15 mins

Resource locking with @ResourceLock in JUnit - Build an Automation Script

Choose your learning style9 modes available
Test resource locking using @ResourceLock annotation in JUnit
Preconditions (2)
Step 1: Create a test class with two test methods
Step 2: Annotate both test methods with @ResourceLock using the same resource name
Step 3: Run the tests in parallel mode
Step 4: Observe that the tests do not run concurrently on the locked resource
✅ Expected Result: The two test methods run sequentially, not in parallel, because the shared resource is locked by @ResourceLock
Automation Requirements - JUnit 5.9+
Assertions Needed:
Verify that both test methods complete successfully
Verify that the resource lock prevents concurrent execution
Best Practices:
Use @ResourceLock with a clear resource name
Enable parallel execution in JUnit configuration
Use assertions to confirm test completion
Avoid hardcoding thread sleeps; rely on JUnit's locking
Automated Solution
JUnit
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.parallel.Execution;
import org.junit.jupiter.api.parallel.ExecutionMode;
import org.junit.jupiter.api.parallel.ResourceLock;

import static org.junit.jupiter.api.Assertions.assertTrue;

@Execution(ExecutionMode.CONCURRENT)
public class ResourceLockTest {

    private static volatile boolean resourceInUse = false;

    @Test
    @ResourceLock("SHARED_RESOURCE")
    void testMethodOne() throws InterruptedException {
        assertTrue(acquireResource());
        // Simulate work with the resource
        Thread.sleep(200);
        releaseResource();
    }

    @Test
    @ResourceLock("SHARED_RESOURCE")
    void testMethodTwo() throws InterruptedException {
        assertTrue(acquireResource());
        // Simulate work with the resource
        Thread.sleep(200);
        releaseResource();
    }

    private synchronized boolean acquireResource() {
        if (resourceInUse) {
            return false; // resource already in use
        }
        resourceInUse = true;
        return true;
    }

    private synchronized void releaseResource() {
        resourceInUse = false;
    }
}

This test class uses @Execution(ExecutionMode.CONCURRENT) to allow parallel test execution.

Both test methods are annotated with @ResourceLock("SHARED_RESOURCE") to lock the same resource name.

The resourceInUse flag simulates the shared resource state. The acquireResource() method ensures only one test can use the resource at a time.

Each test sleeps for 200ms to simulate work, then releases the resource.

Assertions check that the resource was successfully acquired, confirming the lock works.

Because of @ResourceLock, JUnit runs these tests sequentially on the shared resource, preventing concurrent access.

Common Mistakes - 3 Pitfalls
Not enabling parallel execution in JUnit configuration
Using different resource names in @ResourceLock annotations
Using Thread.sleep() without proper synchronization
Bonus Challenge

Now add a third test method that locks a different resource and verify it runs concurrently with the others.

Show Hint