1.2.5.14. Keeping State in Extensions
Usually, an extension is instantiated only once. So the question becomes relevant: How do you keep the state from one invocation of an extension to the next? The ExtensionContext
API provides a Store
exactly for this purpose. Extensions may put values into a store for later retrieval. See the TimingExtension
for an example of using the Store
with a method-level scope. It is important to remember that values stored in an ExtensionContext
during test execution will not be available in the surrounding ExtensionContext
. Since ExtensionContexts
may be nested, the scope of inner contexts may also be limited. Consult the corresponding Javadoc for details on the methods available for storing and retrieving values via the Store
.
ExtensionContext.Store.CloseableResource
An extension context store is bound to its extension context lifecycle. When an extension context lifecycle ends it closes its associated store. All stored values that are instances of
CloseableResource
are notified by an invocation of theirclose()
method in the inverse order they were added in.