Skip to content

Commit 3246321

Browse files
committed
Fully document that @⁠TempDir can be used on constructor parameters
This commit is a followup to #4060. Closes: #4285
1 parent f45b882 commit 3246321

File tree

3 files changed

+18
-21
lines changed

3 files changed

+18
-21
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ in the `junit-jupiter-api` module.
4747
| `@Disabled` | Used to <<writing-tests-disabling,disable>> a test class or test method; analogous to JUnit 4's `@Ignore`. Such annotations are not inherited.
4848
| `@AutoClose` | Denotes that the annotated field represents a resource that will be <<writing-tests-built-in-extensions-AutoClose,automatically closed>> after test execution.
4949
| `@Timeout` | Used to fail a test, test factory, test template, or lifecycle method if its execution exceeds a given duration. Such annotations are inherited.
50-
| `@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. Such fields are inherited.
50+
| `@TempDir` | Used to supply a <<writing-tests-built-in-extensions-TempDirectory,temporary directory>> via field injection or parameter injection in a test class constructor, lifecycle method, or test method; located in the `org.junit.jupiter.api.io` package. Such fields are inherited.
5151
| `@ExtendWith` | Used to <<extensions-registration-declarative,register extensions declaratively>>. Such annotations are inherited.
5252
| `@RegisterExtension` | Used to <<extensions-registration-programmatic,register extensions programmatically>> via fields. Such fields are inherited.
5353
|===

junit-jupiter-api/src/main/java/org/junit/jupiter/api/io/TempDir.java

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -28,21 +28,16 @@
2828
import org.junit.jupiter.api.extension.ParameterResolutionException;
2929

3030
/**
31-
* {@code @TempDir} can be used to annotate a field in a test class or a
32-
* parameter in a lifecycle method or test method of type {@link Path} or
33-
* {@link File} that should be resolved into a temporary directory.
34-
*
35-
* <p>Please note that {@code @TempDir} is not supported on constructor
36-
* parameters. Please use field injection instead by annotating an instance
37-
* field with {@code @TempDir}.
31+
* {@code @TempDir} can be used to annotate a field in a test class or a parameter
32+
* in a test class constructor, lifecycle method, or test method of type
33+
* {@link Path} or {@link File} that should be resolved into a temporary directory.
3834
*
3935
* <h2>Creation</h2>
4036
*
4137
* <p>The temporary directory is only created if a field in a test class or a
42-
* parameter in a lifecycle method or test method is annotated with
43-
* {@code @TempDir}.
44-
* An {@link ExtensionConfigurationException} or a
45-
* {@link ParameterResolutionException} will be thrown in one of the following
38+
* parameter in a test class constructor, lifecycle method, or test method is
39+
* annotated with {@code @TempDir}. An {@link ExtensionConfigurationException} or
40+
* a {@link ParameterResolutionException} will be thrown in one of the following
4641
* cases:
4742
*
4843
* <ul>
@@ -56,15 +51,16 @@
5651
* </li>
5752
* </ul>
5853
*
59-
* In addition, a {@code ParameterResolutionException} will be thrown for a
60-
* constructor parameter annotated with {@code @TempDir}.
61-
*
6254
* <h2>Scope</h2>
6355
*
64-
* <p>By default, a separate temporary directory is created for every
65-
* declaration of the {@code @TempDir} annotation. If you want to share a
66-
* temporary directory across all tests in a test class, you should declare the
67-
* annotation on a {@code static} field or on a parameter of a
56+
* <p>By default, a separate temporary directory is created for every declaration
57+
* of the {@code @TempDir} annotation. For better isolation when using
58+
* {@link org.junit.jupiter.api.TestInstance.Lifecycle#PER_METHOD @TestInstance(Lifecycle.PER_METHOD)}
59+
* semantics, you can annotate an instance field or a parameter in the test class
60+
* constructor with {@code @TempDir} so that each test method uses a separate
61+
* temporary directory. Alternatively, if you want to share a temporary directory
62+
* across all tests in a test class, you should declare the annotation on a
63+
* {@code static} field or on a parameter of a
6864
* {@link org.junit.jupiter.api.BeforeAll @BeforeAll} method.
6965
*
7066
* <h3>Old behavior</h3>

junit-jupiter-engine/src/main/java/org/junit/jupiter/engine/extension/TempDirectory.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,9 @@
7272

7373
/**
7474
* {@code TempDirectory} is a JUnit Jupiter extension that creates and cleans
75-
* up temporary directories if field in a test class or a parameter in a
76-
* lifecycle method or test method is annotated with {@code @TempDir}.
75+
* up temporary directories if a field in a test class or a parameter in a
76+
* test class constructor, lifecycle method, or test method is annotated with
77+
* {@code @TempDir}.
7778
*
7879
* <p>Consult the Javadoc for {@link TempDir} for details on the contract.
7980
*

0 commit comments

Comments
 (0)