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

Implement @AutoClose Annotation #3368

Conversation

ibrahimesseddyq
Copy link

@ibrahimesseddyq ibrahimesseddyq commented Jun 24, 2023

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.

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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant