0
0
JUnittesting~5 mins

Resource locking with @ResourceLock in JUnit - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
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?
ARuns the test only if the 'DB' resource is available
BUnlocks the resource named 'DB' after test execution
CLocks the resource named 'DB' to prevent concurrent access
DMarks the test as dependent on the 'DB' resource
Which ResourceAccessMode allows multiple tests to read the same resource simultaneously?
AREAD
BEXCLUSIVE
CWRITE
DSHARED
If two tests lock the same resource in WRITE mode, what happens?
AOne test waits until the other finishes
BBoth tests run at the same time
CTests fail due to conflict
DTests run in random order
Which annotation is used to lock resources in JUnit 5 for parallel tests?
A@LockResource
B@ResourceLock
C@Synchronized
D@TestLock
Why should you use resource locking in tests that run in parallel?
ATo speed up test execution
BTo run tests sequentially
CTo skip tests that use shared resources
DTo avoid resource conflicts and flaky tests
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.