Skip to content
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

Conversation

ibrahimesseddyq
Copy link

@ibrahimesseddyq ibrahimesseddyq commented Jun 24, 2023

Overview


I hereby agree to the terms of the JUnit Contributor License Agreement.


Definition of Done

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";
Copy link
Contributor

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 than close() 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)`.
Copy link
Contributor

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.

@bjmi
Copy link
Contributor

bjmi commented Jun 29, 2023

A different approach would

  1. find all @AutoClose annotated fields,
  2. wraps them in ExtensionContext.Store.CloseableResource
  3. put them to the provided ExtensionContext.Store

and let the JUnit engine close the resources when the Store is closed.

@marcphilipp
Copy link
Member

@ibrahimesseddyq Do you have time to adjust your PR according to the team decision in #3367 (comment)?

@ibrahimesseddyq
Copy link
Author

@marcphilipp i will be working on the changes from today

@sormuras
Copy link
Member

sormuras commented Dec 4, 2023

Touching over 800 files with +86,253 −2 changes does not sound right.

@ibrahimesseddyq
Copy link
Author

ibrahimesseddyq commented Dec 4, 2023

Touching over 800 files with +86,253 −2 changes does not sound right.

didn't added and commited gradle folder , dont know why that happened , i will correct this

@bjmi bjmi mentioned this pull request Dec 5, 2023
6 tasks
@ibrahimesseddyq
Copy link
Author

ibrahimesseddyq commented Dec 6, 2023

refactored code to functionnal style(all extensions are using this style) & added context.Store
last time error :checkstyleNohttp still here(caused by gradle folder)

@ibrahimesseddyq
Copy link
Author

@sormuras @marcphilipp @bjmi some ideas to fix that gradle folder problem?

@marcphilipp
Copy link
Member

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.

@sbrannen
Copy link
Member

Thanks again for the PR, @ibrahimesseddyq! 👍

Please note, however, that this has now been:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Introduce @AutoClose mechanism for closing field resources after test execution
5 participants