Top Description Methods
org.junit.jupiter.api

public @Interface AutoClose

extends Annotation
Annotations
@Target:ANNOTATION_TYPE, FIELD
@Retention:RUNTIME
@Documented
@API
status:MAINTAINED
since:5.13.3
Static Imports
org.apiguardian.api.API.Status.MAINTAINED

@AutoClose is used to indicate that an annotated field will be automatically closed after test execution.

@AutoClose fields may be either static or non-static. If the value of an @AutoClose field is null when it is evaluated the field will be ignored, but a warning message will be logged to inform you.

By default, @AutoClose expects the value of the annotated field to implement a close() method that will be invoked to close the resource. However, developers can customize the name of the close method via the value attribute. For example, @AutoClose("shutdown") instructs JUnit to look for a shutdown() method to close the resource.

@AutoClose may be used as a meta-annotation in order to create a custom composed annotation that inherits the semantics of @AutoClose.

Inheritance

@AutoClose fields are inherited from superclasses. Furthermore, @AutoClose fields from subclasses will be closed before @AutoClose fields in superclasses.

Evaluation Order

When multiple @AutoClose fields 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 @AutoClose fields implements the AfterAllCallback and TestInstancePreDestroyCallback extension APIs. Consequently, a static @AutoClose field will be closed after all tests in the current test class have completed, effectively after @AfterAll methods have executed for the test class. A non-static @AutoClose field 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 @AutoClose field 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 @AutoClose field will not be closed until the current test class instance is no longer needed, which means after @AfterAll methods and after all static @AutoClose fields have been closed.

Since
5.11

Method Summary

Modifier and TypeMethod and Description
public String

Returns:

the name of the method to invoke to close the resource
value
()

Specify the name of the method to invoke to close the resource.

Inherited from java.lang.annotation.Annotation:
annotationTypeequalshashCodetoString

Method Detail

valueback to summary
public String value()

Specify the name of the method to invoke to close the resource.

The default value is "close" which works with any type that implements AutoCloseable or has a close() method.

Returns:String

the name of the method to invoke to close the resource