0
0
JUnittesting~20 mins

Resource locking with @ResourceLock in JUnit - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Resource Locking Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of concurrent tests with @ResourceLock
Consider the following JUnit 5 test class using @ResourceLock to manage concurrent access. What will be the output order of the printed statements when running all tests in parallel?
JUnit
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.parallel.ResourceLock;
import org.junit.jupiter.api.parallel.Resources;
import org.junit.jupiter.api.parallel.Execution;
import org.junit.jupiter.api.parallel.ExecutionMode;

@Execution(ExecutionMode.CONCURRENT)
public class ResourceLockTest {

    @Test
    @ResourceLock(value = "RESOURCE_A")
    void test1() throws InterruptedException {
        System.out.println("Start test1");
        Thread.sleep(100);
        System.out.println("End test1");
    }

    @Test
    @ResourceLock(value = "RESOURCE_A")
    void test2() throws InterruptedException {
        System.out.println("Start test2");
        Thread.sleep(100);
        System.out.println("End test2");
    }

    @Test
    void test3() {
        System.out.println("Start test3");
        System.out.println("End test3");
    }
}
AStart test3\nStart test1\nStart test2\nEnd test3\nEnd test1\nEnd test2
BStart test1\nStart test2\nEnd test1\nEnd test2\nStart test3\nEnd test3
CStart test1\nEnd test1\nStart test2\nEnd test2\nStart test3\nEnd test3
DStart test3\nEnd test3\nStart test1\nEnd test1\nStart test2\nEnd test2
Attempts:
2 left
💡 Hint
Remember that tests locking the same resource run sequentially, but tests without locks can run concurrently.
assertion
intermediate
1:30remaining
Correct assertion for resource lock count
You have a test method annotated with @ResourceLock("DB_CONNECTION") that increments a shared counter. Which assertion correctly verifies that the counter increments exactly once per test execution when tests run concurrently?
JUnit
import static org.junit.jupiter.api.Assertions.assertEquals;

int counter = 0;

@Test
@ResourceLock("DB_CONNECTION")
void incrementCounter() {
    counter++;
}
AassertEquals(1, counter);
BassertEquals(2, counter);
CassertNotNull(counter);
DassertTrue(counter >= 1);
Attempts:
2 left
💡 Hint
The resource lock ensures only one test increments at a time, so the counter should be exactly 1 after one test.
🔧 Debug
advanced
2:30remaining
Identify the cause of test deadlock with @ResourceLock
Given two test methods each annotated with @ResourceLock on different resources but run concurrently, the test suite hangs indefinitely. What is the most likely cause?
JUnit
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.parallel.ResourceLock;
import org.junit.jupiter.api.parallel.Execution;
import org.junit.jupiter.api.parallel.ExecutionMode;

@Execution(ExecutionMode.CONCURRENT)
public class DeadlockTest {

    @Test
    @ResourceLock(value = "RESOURCE_1")
    void testA() {
        // locks RESOURCE_1
        // then tries to lock RESOURCE_2 inside
    }

    @Test
    @ResourceLock(value = "RESOURCE_2")
    void testB() {
        // locks RESOURCE_2
        // then tries to lock RESOURCE_1 inside
    }
}
AIncorrect @ResourceLock annotation syntax causing test hang
BJUnit does not support concurrent execution with @ResourceLock
CCircular wait due to nested resource locks causing deadlock
DTests are not annotated with @Execution(ExecutionMode.SAME_THREAD)
Attempts:
2 left
💡 Hint
Think about what happens when two tests wait on each other's locked resources.
framework
advanced
1:30remaining
Behavior of @ResourceLock with multiple resources
What happens when a test method is annotated with @ResourceLock(value = {"RESOURCE_X", "RESOURCE_Y"}) in JUnit 5?
AThe test locks RESOURCE_X first, then RESOURCE_Y sequentially during execution
BThe test locks both RESOURCE_X and RESOURCE_Y simultaneously before execution
CThe test locks only RESOURCE_X and ignores RESOURCE_Y
DThe test locks RESOURCE_X and RESOURCE_Y but allows other tests to lock them concurrently
Attempts:
2 left
💡 Hint
Consider how @ResourceLock handles multiple resource names in its value array.
🧠 Conceptual
expert
3:00remaining
Effect of @ResourceLock on test execution order and concurrency
In a large test suite with many tests annotated with different @ResourceLock values, how does JUnit 5 determine which tests can run concurrently and which must run sequentially?
ATests locking the same resource run sequentially; tests locking different resources run concurrently
BAll tests run sequentially regardless of resource locks to avoid conflicts
CTests run concurrently only if they have no @ResourceLock annotation
DJUnit randomly schedules tests ignoring resource locks
Attempts:
2 left
💡 Hint
Think about resource locking as a way to prevent simultaneous access to shared resources.