Annotation Type ResourceLock
-
@API(status=STABLE, since="5.10") @Retention(RUNTIME) @Target({TYPE,METHOD}) @Inherited @Repeatable(ResourceLocks.class) public @interface ResourceLock@ResourceLockis used to declare that the annotated test class or test method requires access to a shared resource identified by a key.The resource key is specified via
value(). In addition,mode()allows one to specify whether the annotated test class or test method requiresREAD_WRITEorREADaccess to the resource. In the former case, execution of the annotated element will occur while no other test class or test method that uses the shared resource is being executed. In the latter case, the annotated element may be executed concurrently with other test classes or methods that also requireREADaccess but not at the same time as any other test that requiresREAD_WRITEaccess.This guarantee extends to lifecycle methods of a test class or method. For example, if a test method is annotated with
@ResourceLockthe lock will be acquired before any@BeforeEachmethods are executed and released after all@AfterEachmethods have been executed.This annotation can be repeated to declare the use of multiple shared resources.
Uniqueness of a shared resource is determined by both the
value()and themode(). Duplicated shared resources do not cause errors.Since JUnit Jupiter 5.4, this annotation is inherited within class hierarchies.
Since JUnit Jupiter 5.12, this annotation supports adding shared resources dynamically at runtime via
providers(). Resources declared "statically" usingvalue()andmode()are combined with "dynamic" resources added viaproviders(). For example, declaring resource "A" via@ResourceLock("A")and resource "B" via a provider returningnew Lock("B")will result in two shared resources "A" and "B".Since JUnit Jupiter 5.12, this annotation supports declaring "static" shared resources for direct child nodes via the
target()attribute. UsingResourceLockTarget.CHILDRENin a class-level annotation has the same semantics as adding an annotation with the samevalue()andmode()to each test method and nested test class declared in the annotated class. This may improve parallelization when a test class declares aREADlock, but only a few methods holdREAD_WRITElock. Note thattarget = CHILDRENmeans thatvalue()andmode()no longer apply to a node declaring the annotation. However, theproviders()attribute remains applicable, and the target of "dynamic" shared resources added via implementations ofResourceLocksProvideris not changed.Shared resources declared on or provided for methods or nested test classes in a
@ClassTemplateare propagated as if they were declared on the outermost enclosing@ClassTemplateclass itself.- Since:
- 5.3
- See Also:
Isolated,Resources,ResourceAccessMode,ResourceLockTarget,ResourceLocks,ResourceLocksProvider
-
-
Optional Element Summary
Optional Elements Modifier and Type Optional Element Description ResourceAccessModemodeThe resource access mode.java.lang.Class<? extends ResourceLocksProvider>[]providersAn array of one or more classes implementingResourceLocksProvider.ResourceLockTargettargetjava.lang.StringvalueThe resource key.
-
-
-
Element Detail
-
value
java.lang.String value
The resource key.Defaults to an empty string.
- See Also:
Resources,ResourceLocksProvider.Lock.getKey()
- Default:
- ""
-
-
-
mode
ResourceAccessMode mode
The resource access mode.Defaults to
READ_WRITE.- Default:
- org.junit.jupiter.api.parallel.ResourceAccessMode.READ_WRITE
-
-
-
providers
@API(status=MAINTAINED, since="5.13.3") java.lang.Class<? extends ResourceLocksProvider>[] providersAn array of one or more classes implementingResourceLocksProvider.Defaults to an empty array.
- Since:
- 5.12
- See Also:
ResourceLocksProvider.Lock
- Default:
- {}
-
-
-
target
@API(status=MAINTAINED, since="5.13.3") ResourceLockTarget targetThe target of a resource created fromvalue()andmode().Defaults to
SELF.Note that using
ResourceLockTarget.CHILDRENin a method-level annotation results in an exception.- Since:
- 5.12
- See Also:
ResourceLockTarget
- Default:
- org.junit.jupiter.api.parallel.ResourceLockTarget.SELF
-
-