-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Introduce @AutoClose
mechanism for closing field resources after test execution
#3367
Comments
@AutoClose
mechanism for closing resources after test execution
what's about the order several resources close? |
I have no strong opinion about it. Two thoughts:
|
Team decision: Introduce attribute-less |
Limiting to
A similar solution are Spring Beans destroy methods ( Therefore I propose following annotation and just search for a public and no-arg method and disregard the type of the field.
I could provide a solution that uses |
Supporting
|
Thanks for the tip. Unfortunately, this doesn't help users who are dealing with previous versions. |
@AutoClose
mechanism for closing resources after test execution@AutoClose
mechanism for closing field resources after test execution
Signed-off-by: Björn Michael <b.michael@gmx.de> See junit-team#3367 See junit-team#3592
We have decided to support a configurable close-method name which defaults to In addition, due to the nature of our reflection utilities, we do in fact provide some guarantees regarding ordering, as I've outlined in the Javadoc for
|
Signed-off-by: Björn Michael <b.michael@gmx.de> See junit-team#3367 See junit-team#3592
Signed-off-by: Björn Michael <b.michael@gmx.de> See junit-team#3367 See junit-team#3592
This commit introduces an @AutoClose annotation that can be applied to fields within JUnit Jupiter tests to automatically close the annotated resource after test execution. Signed-off-by: Björn Michael <b.michael@gmx.de> See junit-team#3367 See junit-team#3592
This commit revises the @AutoClose support that was introduced in the previous commit as follows. - Javadoc and User Guide documentation for @AutoClose, now explicitly explain ordering, inheritance, evaluation order, scope, and lifecycle for @AutoClose fields. - Overhauled and simplified the implementation of the AutoCloseExtension by closing fields immediately instead of wrapping each field in a CloseableResource and storing it in the ExtensionContext.Store, and revised the implementation of preDestroyTestInstance() so that it makes use of TestInstancePreDestroyCallback.preDestroyTestInstances() to ensure proper semantics within @Nested test class hierarchies. Combined, these changes ensure that fields are closed in the proper order and that any resulting exceptions are thrown directly instead of nested as a cause of an exception that occurs when the ExtensionContext.Store is closed. - Improved diagnostics by failing eagerly if an @AutoClose annotation is declared with a blank close-method name or if the annotated field is a primitive type or array. - Since having the AutoCloseExtension in the org.junit.jupiter.api package introduced a cycle between the api and api.extension packages, the AutoCloseExtension now resides in the org.junit.jupiter.engine.extension package in junit-jupiter-engine and is loaded as a built-in extension. - The AutoCloseDemo now uses a WebClient that needs to be closed instead of a JDBC Connection, thereby avoiding the need to throw an SQLException and simplifying the example. - Overhauled AutoCloseTests to test all known use cases (including test class hierarchies and @Nested test class hierarchies) and error cases. Closes junit-team#3367 Closes junit-team#3592
Reopening to make use of |
RE-Reopening to make sure that the use of |
Occasionally I create and use resources in unit tests that should be released after test execution. You usually do this in tear-down methods. But in a lot of cases a simple
@AutoClose
annotated field would be sufficient for that, something similar to@TempDir
for temporary folders.This would spare boilerplate code like:
Allowing it to be replaced by:
Static fields would be closed after all tests have run and instance fields after each test.
Other annotation names could be
@Destroy
or@AutoDestroy
.If there is no
close()
method, it should be possible to specify a custom method name -- for example,destroy
,dispose
,shutdown
, etc.Deliverables
@AutoClose
annotationThe text was updated successfully, but these errors were encountered: