Replies: 1 comment
-
|
At first review, this proposal seems useful but also non-trivial to with respect to the order in which dependencies are closed and the collections that are supported. As such it would be prudent to incubate this first. Have you considered writing a JUnit Jupiter extension? Similar to the current If this seems feasible, you could then propose it to the JUnit Pioneer project and incubate it there. I imagine that for the end user it might look something like this: @AutoCloseCollection(order = REVERSE)
private List<AutoCloseable> resources = new ArrayList<>();
@Test
void test() {
Group group = createGroup();
resources.add(() -> deleteGroup(group));
...
// Automatically closed in LIFO order: user2, user1, group
} |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
First off, congrats on the 6.0 release! I've been using JUnit for over 20 years and it keeps getting better. Thanks for all the great work.
I've been looking for a good way to clean up multiple test resources, and thought
@AutoClosemight be a good fit. Here's an idea that might be useful for others too.Motivation
@AutoClosecurrently works well for test fixtures declared as fields, but doesn't seem designed for multiple resources created dynamically within test methods. Tests often need to create multiple resources and ensure they're all cleaned up. Since these resources typically have dependencies, cleanup needs to happen in reverse order of creation (LIFO).Current workaround:
Proposal
Allow
@AutoCloseto work withList:Resources are closed in LIFO order (reverse of addition). Even if closing fails, all resources are still closed (exceptions are added as suppressed).
Why This Helps
@TempDirusingPathWould this be useful for your use cases?
I've shown
Listin the proposal, but would other collection interfaces likeDequebe better suited for this? (With Java 21,SequencedCollectionwould be ideal, but JUnit currently targets Java 17.)This proposal relates to #3367 where ordering was deferred "for now".
This idea is inspired by the Automated Teardown pattern from Gerard Meszaros's xUnit Test Patterns.
Happy to contribute a PR if there's interest!
Beta Was this translation helpful? Give feedback.
All reactions