Annotation Type AutoClose
-
@Target({ANNOTATION_TYPE,FIELD}) @Retention(RUNTIME) @Documented @API(status=MAINTAINED, since="5.13.3") public @interface AutoClose@AutoCloseis used to indicate that an annotated field will be automatically closed after test execution.@AutoClosefields may be eitherstaticor non-static. If the value of an@AutoClosefield isnullwhen it is evaluated the field will be ignored, but a warning message will be logged to inform you.By default,
@AutoCloseexpects the value of the annotated field to implement aclose()method that will be invoked to close the resource. However, developers can customize the name of theclosemethod via thevalue()attribute. For example,@AutoClose("shutdown")instructs JUnit to look for ashutdown()method to close the resource.@AutoClosemay be used as a meta-annotation in order to create a custom composed annotation that inherits the semantics of@AutoClose.Inheritance
@AutoClosefields are inherited from superclasses. Furthermore,@AutoClosefields from subclasses will be closed before@AutoClosefields in superclasses.Evaluation Order
When multiple
@AutoClosefields exist within a given test class, the order in which the resources are closed depends on an algorithm that is deterministic but intentionally nonobvious. This ensures that subsequent runs of a test suite close resources in the same order, thereby allowing for repeatable builds.Scope and Lifecycle
The extension that closes
@AutoClosefields implements theAfterAllCallbackandTestInstancePreDestroyCallbackextension APIs. Consequently, astatic@AutoClosefield will be closed after all tests in the current test class have completed, effectively after@AfterAllmethods have executed for the test class. A non-static@AutoClosefield will be closed before the current test class instance is destroyed. Specifically, if the test class is configured with@TestInstance(Lifecycle.PER_METHOD)semantics, a non-static@AutoClosefield will be closed after the execution of each test method, test factory method, or test template method. However, if the test class is configured with@TestInstance(Lifecycle.PER_CLASS)semantics, a non-static@AutoClosefield will not be closed until the current test class instance is no longer needed, which means after@AfterAllmethods and after allstatic@AutoClosefields have been closed.- Since:
- 5.11
-
-
Optional Element Summary
Optional Elements Modifier and Type Optional Element Description java.lang.StringvalueSpecify the name of the method to invoke to close the resource.
-