Replies: 3 comments 7 replies
-
|
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.
-
|
Thank you for the feedback. After my initial post, I learned that Proposals I am Withdrawing
Current Direction: A Custom
|
Beta Was this translation helpful? Give feedback.
-
|
From I think that "simple, reliable, and easy-to-understand" contradicts "dynamically created dependent resources, or have lifecycle semantics that are not strictly per-test-method." – Especially the non-locality of the latter, not having everything that matters inside the body of a test method, is far from "easy-to-understand" by other folks, including future-you/me. Leaving out the Seems like |
Beta Was this translation helpful? Give feedback.
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