Skip to content

Commit 75538a7

Browse files
committed
Document that autoCloseArguments in @ParameterizedTest is a potentially breaking change
Closes #2706
1 parent af7dc3c commit 75538a7

File tree

3 files changed

+34
-8
lines changed

3 files changed

+34
-8
lines changed

documentation/src/docs/asciidoc/release-notes/release-notes-5.8.0.adoc

+9-2
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,12 @@ on GitHub.
120120

121121
==== Deprecations and Breaking Changes
122122

123+
* The new `autoCloseArguments` feature in `@ParameterizedTest` may potentially be a
124+
breaking change for existing parameterized test methods if an argument that implements
125+
`java.lang.AutoCloseable` is reused for multiple invocations of the same parameterized
126+
test method. If your parameterized test methods start to fail when you upgrade to JUnit
127+
Jupiter 5.8, you can disable this feature by declaring
128+
`@ParameterizedTest(autoCloseArguments = false)`.
123129
* `InvocationInterceptor.interceptDynamicTest(Invocation<Void>, ExtensionContext)` has
124130
been deprecated in favor of
125131
`InvocationInterceptor.interceptDynamicTest(Invocation<Void>, DynamicTestInvocationContext, ExtensionContext)`
@@ -165,8 +171,9 @@ on GitHub.
165171
the information available in a `StackTraceElement`.
166172
* Dynamic tests now require less memory thanks to a number of improvements to internal
167173
data structures.
168-
* New `autoCloseArguments` attribute in `@ParameterizedTest` to close `AutoCloseable`
169-
arguments at the end of the test. This attribute defaults to true.
174+
* New `autoCloseArguments` attribute in `@ParameterizedTest` to close
175+
`java.lang.AutoCloseable` arguments after each invocation of the parameterized test
176+
method. This attribute defaults to `true`.
170177
* Numeric literals used with `@CsvSource` or `CsvFileSource` can now be expressed using
171178
underscores as in some JVM languages, to improve readability of long numbers like
172179
`700_000_000`.

documentation/src/docs/asciidoc/user-guide/writing-tests.adoc

+14-3
Original file line numberDiff line numberDiff line change
@@ -1118,9 +1118,20 @@ parameterized method at the same index in the method's formal parameter list. An
11181118
_aggregator_ is any parameter of type `ArgumentsAccessor` or any parameter annotated with
11191119
`@AggregateWith`.
11201120

1121-
NOTE: Arguments of type `AutoCloseable` will be closed once `@AfterEach` methods and
1122-
`AfterEachCallback` implementations have been called. To prevent this from happening, set
1123-
the `autoCloseArguments` attribute in `@ParameterizedTest` to `false`.
1121+
[NOTE]
1122+
.AutoCloseable arguments
1123+
====
1124+
Arguments that implement `java.lang.AutoCloseable` (or `java.io.Closeable` which extends
1125+
`java.lang.AutoCloseable`) will be automatically closed after `@AfterEach` methods and
1126+
`AfterEachCallback` extensions have been called for the current parameterized test
1127+
invocation.
1128+
1129+
To prevent this from happening, set the `autoCloseArguments` attribute in
1130+
`@ParameterizedTest` to `false`. Specifically, if an argument that implements
1131+
`AutoCloseable` is reused for multiple invocations of the same parameterized test method,
1132+
you must annotate the method with `@ParameterizedTest(autoCloseArguments = false)` to
1133+
ensure that the argument is not closed between invocations.
1134+
====
11241135

11251136
[[writing-tests-parameterized-tests-sources]]
11261137
==== Sources of Arguments

junit-jupiter-params/src/main/java/org/junit/jupiter/params/ParameterizedTest.java

+11-3
Original file line numberDiff line numberDiff line change
@@ -209,9 +209,17 @@
209209
String name() default "{default_display_name}";
210210

211211
/**
212-
* If true, all arguments of the parameterized test implementing {@link AutoCloseable}
213-
* will be closed after {@code @AfterEach} methods and {@code AfterEachCallbacks}
214-
* have been called.
212+
* Configure whether all arguments of the parameterized test that implement {@link AutoCloseable}
213+
* will be closed after {@link org.junit.jupiter.api.AfterEach @AfterEach} methods
214+
* and {@link org.junit.jupiter.api.extension.AfterEachCallback AfterEachCallback}
215+
* extensions have been called for the current parameterized test invocation.
216+
*
217+
* <p>Defaults to {@code true}.
218+
*
219+
* <p><strong>WARNING</strong>: if an argument that implements {@code AutoCloseable}
220+
* is reused for multiple invocations of the same parameterized test method,
221+
* you must set {@code autoCloseArguments} to {@code false} to ensure that
222+
* the argument is not closed between invocations.
215223
*
216224
* @since 5.8
217225
* @see java.lang.AutoCloseable

0 commit comments

Comments
 (0)