-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
Issues/3367 introduce autoclose #3369
Issues/3367 introduce autoclose #3369
Conversation
This commit introduces the @autoclose annotation, AutoCloseUtils class, and AutoCloseExtension class to enhance resource management in JUnit 5 tests. The @autoclose Annotation: The @autoclose annotation is a custom annotation created in the org.junit.jupiter.api package. It can be applied to fields within JUnit 5 test classes. This annotation indicates that the annotated resource should be automatically closed after test execution. It specifically targets ElementType.FIELD elements. AutoCloseUtils Class: The AutoCloseUtils class is added to the org.junit.jupiter.api package. It provides utility methods for automatically closing resources used in tests. The closeResources method takes a test instance as a parameter and closes all fields annotated with @autoclose within that instance. AutoCloseExtension Class: The AutoCloseExtension class is introduced in the org.junit.jupiter.api.extension package. It implements the AfterEachCallback interface and acts as a JUnit 5 extension. By using the @ExtendWith(AutoCloseExtension.class) annotation on test classes or methods, the extension invokes AutoCloseUtils.closeResources to automatically close resources annotated with @autoclose after each test execution. These changes aim to simplify resource cleanup in JUnit 5 tests, reduce boilerplate code, and enhance the readability of test code. Please review the introduced changes and provide any feedback or suggestions for improvement.
* | ||
* @return the method name for closing the resource | ||
*/ | ||
String value() default "close"; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
- If it is allowed to specify a different method name then the annotated resource does not necessarily have to implement
java.io.Closeable
. value()
isn't evaluated and so a different method other thanclose()
isn't invoked.
@@ -45,6 +45,7 @@ in the `junit-jupiter-api` module. | |||
| `@ExtendWith` | Used to <<extensions-registration-declarative,register extensions declaratively>>. Such annotations are _inherited_. | |||
| `@RegisterExtension` | Used to <<extensions-registration-programmatic,register extensions programmatically>> via fields. Such fields are _inherited_ unless they are _shadowed_. | |||
| `@TempDir` | Used to supply a <<writing-tests-built-in-extensions-TempDirectory,temporary directory>> via field injection or parameter injection in a lifecycle method or test method; located in the `org.junit.jupiter.api.io` package. | |||
| @AutoClose | Indicates that a field in a JUnit 5 test class represents a resource that should be automatically closed after test execution. The field must implement the java.io.Closeable interface. The class must be annotated with `@ExtendWith(AutoCloseExtension.class)`. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It should work out of the box like @TempDir
without specifying @ExtendWith(AutoCloseExtension.class)
explicitly.
As @AutoClose
is an opt-in feature it should not interfere with other extension if this extension isn't used but registered.
A different approach would
and let the JUnit engine close the resources when the Store is closed. |
@ibrahimesseddyq Do you have time to adjust your PR according to the team decision in #3367 (comment)? |
@marcphilipp i will be working on the changes from today |
Touching over 800 files with |
didn't added and commited gradle folder , dont know why that happened , i will correct this |
This reverts commit 588ad9a.
…e and adding contextStore]
refactored code to functionnal style(all extensions are using this style) & added context.Store |
@sormuras @marcphilipp @bjmi some ideas to fix that gradle folder problem? |
I'd recommend rebasing and changing the commit that added the gradle folder. However, there's another PR for this issue by @bjmi now so we should first decide for one before making you do additional work. |
Thanks again for the PR, @ibrahimesseddyq! 👍 Please note, however, that this has now been:
|
Overview
I hereby agree to the terms of the JUnit Contributor License Agreement.
Definition of Done
@API
annotations