0
0
JUnittesting~10 mins

Resource locking with @ResourceLock in JUnit - Test Execution Trace

Choose your learning style9 modes available
Test Overview

This test demonstrates how to use @ResourceLock in JUnit to prevent concurrent access to a shared resource. It verifies that two test methods do not run at the same time on the same resource, avoiding conflicts.

Test Code
JUnit
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.parallel.ResourceLock;
import org.junit.jupiter.api.parallel.Resources;
import static org.junit.jupiter.api.Assertions.assertTrue;

public class ResourceLockTest {

    private static int sharedCounter = 0;

    @Test
    @ResourceLock(value = Resources.SYSTEM_PROPERTIES, mode = ResourceLock.Mode.READ)
    void testReadLock() throws InterruptedException {
        int before = sharedCounter;
        Thread.sleep(100); // Simulate read operation
        int after = sharedCounter;
        assertTrue(before == after, "Shared counter should not change during read lock");
    }

    @Test
    @ResourceLock(value = Resources.SYSTEM_PROPERTIES, mode = ResourceLock.Mode.WRITE)
    void testWriteLock() throws InterruptedException {
        int before = sharedCounter;
        sharedCounter++;
        Thread.sleep(100); // Simulate write operation
        int after = sharedCounter;
        assertTrue(after == before + 1, "Shared counter should increment by 1 during write lock");
    }
}
Execution Trace - 10 Steps
StepActionSystem StateAssertionResult
1Test runner starts and prepares to run testReadLockNo tests running, sharedCounter = 0-PASS
2testReadLock acquires READ lock on SYSTEM_PROPERTIES resourceLock acquired, testReadLock running-PASS
3testReadLock reads sharedCounter value (0), sleeps 100ms simulating readsharedCounter = 0, testReadLock active-PASS
4testReadLock asserts sharedCounter unchanged during read locksharedCounter still 0assertTrue(before == after)PASS
5testReadLock releases READ lockNo locks held-PASS
6Test runner starts testWriteLockNo tests running, sharedCounter = 0-PASS
7testWriteLock acquires WRITE lock on SYSTEM_PROPERTIES resourceLock acquired, testWriteLock running-PASS
8testWriteLock reads sharedCounter (0), increments it to 1, sleeps 100ms simulating writesharedCounter = 1, testWriteLock active-PASS
9testWriteLock asserts sharedCounter incremented by 1 during write locksharedCounter = 1assertTrue(after == before + 1)PASS
10testWriteLock releases WRITE lockNo locks held-PASS
Failure Scenario
Failing Condition: If the resource lock is not respected and both tests run concurrently, sharedCounter may change unexpectedly during read lock.
Execution Trace Quiz - 3 Questions
Test your understanding
What does @ResourceLock prevent in these tests?
AAssertions from failing
BTests from running at all
CConcurrent access to the same resource
DThe sharedCounter from incrementing
Key Result
Using @ResourceLock in JUnit helps avoid race conditions by controlling access to shared resources during parallel test execution.