Resource locking helps tests avoid conflicts when they use the same resource at the same time. It makes tests run safely without breaking each other.
Resource locking with @ResourceLock in JUnit
Introduction
Syntax
JUnit
@ResourceLock(value = "resourceName", mode = ResourceAccessMode.READ) or @ResourceLock(value = "resourceName", mode = ResourceAccessMode.WRITE)
Use value to name the shared resource.
Use mode to specify if the test reads or writes the resource.
Examples
JUnit
@ResourceLock(value = "database", mode = ResourceAccessMode.WRITE)
void testUpdateDatabase() { ... }JUnit
@ResourceLock(value = "configFile", mode = ResourceAccessMode.READ)
void testReadConfig() { ... }Sample Program
This example shows two tests using the same resource "sharedFile". One test locks it for writing, the other for reading. JUnit will run them safely so they don't conflict.
JUnit
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.parallel.ResourceLock; import org.junit.jupiter.api.parallel.ResourceAccessMode; public class ResourceLockExample { @Test @ResourceLock(value = "sharedFile", mode = ResourceAccessMode.WRITE) void testWriteToFile() { System.out.println("Writing to shared file"); // simulate writing } @Test @ResourceLock(value = "sharedFile", mode = ResourceAccessMode.READ) void testReadFromFile() { System.out.println("Reading from shared file"); // simulate reading } }
Important Notes
Use WRITE mode when the test changes the resource.
Use READ mode when the test only reads the resource.
Tests with WRITE lock block other tests with READ or WRITE locks on the same resource.
Summary
@ResourceLock prevents tests from interfering when sharing resources.
Use resource names and access modes to control test access.
This helps tests run safely in parallel without errors.
Practice
1. What is the main purpose of using
@ResourceLock in JUnit tests?easy
Solution
Step 1: Understand the role of @ResourceLock
@ResourceLock is used to control access to shared resources in tests to avoid conflicts.Step 2: Identify the main purpose
Its main purpose is to prevent tests from interfering with each other when they share resources.Final Answer:
To prevent tests from interfering when sharing resources -> Option BQuick Check:
@ResourceLock prevents interference = C [OK]
Hint: Remember: @ResourceLock controls shared resource access [OK]
Common Mistakes:
- Thinking @ResourceLock speeds up tests
- Confusing @ResourceLock with test disabling
- Assuming it generates test data automatically
2. Which of the following is the correct syntax to lock a resource named "database" for read access using
@ResourceLock?easy
Solution
Step 1: Recall the correct annotation syntax
The correct syntax uses 'value' for resource name and 'mode' for access mode with enum ResourceAccessMode.Step 2: Match options with correct syntax
@ResourceLock(value = "database", mode = ResourceAccessMode.READ) matches the correct syntax: @ResourceLock(value = "database", mode = ResourceAccessMode.READ).Final Answer:
@ResourceLock(value = "database", mode = ResourceAccessMode.READ) -> Option CQuick Check:
Correct syntax uses value and mode = A [OK]
Hint: Use value=resourceName and mode=ResourceAccessMode.[READ/WRITE] [OK]
Common Mistakes:
- Using wrong attribute names like resource or name
- Not using ResourceAccessMode enum
- Omitting enum prefix for mode
3. Given the following test methods, what will happen when they run in parallel?
@ResourceLock(value = "file", mode = ResourceAccessMode.READ)
void testReadFile() { /* read file */ }
@ResourceLock(value = "file", mode = ResourceAccessMode.WRITE)
void testWriteFile() { /* write file */ }medium
Solution
Step 1: Understand resource locking modes
READ mode allows multiple readers simultaneously, WRITE mode requires exclusive access.Step 2: Analyze parallel execution with conflicting modes
Since one test locks for READ and the other for WRITE on the same resource, they cannot run at the same time.Final Answer:
testReadFile and testWriteFile will run sequentially to avoid conflicts -> Option AQuick Check:
READ + WRITE locks cause sequential run = A [OK]
Hint: READ and WRITE locks on same resource run sequentially [OK]
Common Mistakes:
- Assuming READ and WRITE locks allow parallel runs
- Thinking tests will fail due to locking
- Believing WRITE lock allows multiple writers
4. Identify the error in the following test code using
@ResourceLock:@ResourceLock(value = "cache", mode = "READ")
void testCacheRead() { /* test code */ }medium
Solution
Step 1: Check the mode attribute type
The mode attribute requires a ResourceAccessMode enum value, not a string.Step 2: Identify the error in the code
Using "READ" as a string causes a syntax error; it should be ResourceAccessMode.READ.Final Answer:
The mode value should be an enum, not a string -> Option AQuick Check:
mode must be enum, not string = B [OK]
Hint: Use ResourceAccessMode.READ, not "READ" string [OK]
Common Mistakes:
- Passing mode as a string instead of enum
- Renaming value attribute incorrectly
- Thinking method must be static
5. You have two test methods:
How will JUnit handle these tests when running in parallel?
@ResourceLock(value = "service", mode = ResourceAccessMode.READ)
void testServiceRead() { /* read operation */ }
@ResourceLock(value = "service", mode = ResourceAccessMode.READ)
void testAnotherRead() { /* another read */ }
@ResourceLock(value = "service", mode = ResourceAccessMode.WRITE)
void testServiceWrite() { /* write operation */ }How will JUnit handle these tests when running in parallel?
hard
Solution
Step 1: Understand read locks behavior
Multiple tests with READ locks on the same resource can run simultaneously.Step 2: Understand write lock behavior
WRITE lock requires exclusive access, so it must wait until all READ locks finish.Step 3: Determine execution order
Both read tests run in parallel; the write test runs alone after reads complete.Final Answer:
Both read tests run in parallel; write test runs alone after reads finish -> Option DQuick Check:
Multiple READs parallel, WRITE exclusive = D [OK]
Hint: Multiple READs run together; WRITE waits for exclusivity [OK]
Common Mistakes:
- Assuming WRITE runs before READs
- Thinking all tests run simultaneously
- Believing deadlock occurs automatically
Recommended
Ready to prove it?
Pass the test and this pattern moves unknown
10 questions ~5 mineasy hard
