@@ -96,6 +96,7 @@ void resetStaticVariables() {
9696 BaseSharedTempDirParameterInjectionTestCase .tempDir = null ;
9797 BaseSeparateTempDirsFieldInjectionTestCase .tempDirs .clear ();
9898 BaseSeparateTempDirsParameterInjectionTestCase .tempDirs .clear ();
99+ BaseConstructorInjectionTestCase .tempDirs .clear ();
99100 }
100101
101102 @ Test
@@ -217,6 +218,17 @@ void resolvesSharedTempDirWhenAnnotationIsUsedOnBeforeAllMethodParameterWithTest
217218 AnnotationOnBeforeAllMethodParameterWithTestInstancePerClassTestCase .class );
218219 }
219220
221+ @ Test
222+ @ DisplayName ("when @TempDir is used on constructor parameter with @TestInstance(PER_CLASS)" )
223+ @ Order (25 )
224+ void resolvesSharedTempDirWhenAnnotationIsUsedOnConstructorWithTestInstancePerClass () {
225+ var results = executeTestsForClass (SharedTempDirsConstructorInjectionPerClassTestCase .class );
226+
227+ results .testEvents ().assertStatistics (stats -> stats .started (2 ).failed (0 ).succeeded (2 ));
228+ assertThat (BaseConstructorInjectionTestCase .tempDirs .getFirst ()).doesNotExist ();
229+ assertThat (BaseConstructorInjectionTestCase .tempDirs .getLast ()).doesNotExist ();
230+ }
231+
220232 private void assertSharedTempDirForFieldInjection (
221233 Class <? extends BaseSharedTempDirFieldInjectionTestCase > testClass ) {
222234
@@ -296,6 +308,17 @@ void resolvesSeparateTempDirWhenAnnotationIsUsedOnAfterAllMethodParameterOnly()
296308 assertThat (AnnotationOnAfterAllMethodParameterTestCase .secondTempDir ).isNotNull ().doesNotExist ();
297309 }
298310
311+ @ Test
312+ @ DisplayName ("when @TempDir is used on constructor parameter" )
313+ @ Order (32 )
314+ void resolvesSeparateTempDirsWhenAnnotationIsUsedOnConstructorWithTestInstancePerMethod () {
315+ var results = executeTestsForClass (SeparateTempDirsConstructorInjectionPerMethodTestCase .class );
316+
317+ results .testEvents ().assertStatistics (stats -> stats .started (2 ).failed (0 ).succeeded (2 ));
318+ assertThat (BaseConstructorInjectionTestCase .tempDirs .getFirst ()).doesNotExist ();
319+ assertThat (BaseConstructorInjectionTestCase .tempDirs .getLast ()).doesNotExist ();
320+ }
321+
299322 }
300323
301324 @ Nested
@@ -370,26 +393,6 @@ void onlySupportsParametersOfTypePathAndFile() {
370393 // @formatter:on
371394 }
372395
373- @ Test
374- @ DisplayName ("when @TempDir is used on constructor parameter" )
375- @ Order (30 )
376- void doesNotSupportTempDirAnnotationOnConstructorParameter () {
377- var results = executeTestsForClass (AnnotationOnConstructorParameterTestCase .class );
378-
379- assertSingleFailedTest (results , ParameterResolutionException .class ,
380- "@TempDir is not supported on constructor parameters. Please use field injection instead." );
381- }
382-
383- @ Test
384- @ DisplayName ("when @TempDir is used on constructor parameter with @TestInstance(PER_CLASS)" )
385- @ Order (31 )
386- void doesNotSupportTempDirAnnotationOnConstructorParameterWithTestInstancePerClass () {
387- var results = executeTestsForClass (AnnotationOnConstructorParameterWithTestInstancePerClassTestCase .class );
388-
389- assertSingleFailedContainer (results , ParameterResolutionException .class ,
390- "@TempDir is not supported on constructor parameters. Please use field injection instead." );
391- }
392-
393396 @ Test
394397 @ DisplayName ("when non-default @TempDir factory is set" )
395398 @ Order (32 )
@@ -693,26 +696,75 @@ static void check(Path tempDir) {
693696
694697 }
695698
696- static class AnnotationOnConstructorParameterTestCase {
699+ static class BaseConstructorInjectionTestCase {
697700
698- AnnotationOnConstructorParameterTestCase (@ SuppressWarnings ("unused" ) @ TempDir Path tempDir ) {
699- // never called
701+ static final Deque <Path > tempDirs = new LinkedList <>();
702+
703+ private final Path tempDir ;
704+
705+ BaseConstructorInjectionTestCase (Path tempDir ) {
706+ this .tempDir = tempDir ;
700707 }
701708
702709 @ Test
703- void test () {
704- // never called
710+ void test1 (@ TempDir Path tempDir , TestInfo testInfo ) throws Exception {
711+ check (tempDir );
712+ writeFile (tempDir , testInfo );
713+ }
714+
715+ @ Test
716+ void test2 (TestInfo testInfo , @ TempDir Path tempDir ) throws Exception {
717+ check (tempDir );
718+ writeFile (tempDir , testInfo );
719+ }
720+
721+ @ AfterEach
722+ void afterEach (@ TempDir Path tempDir ) {
723+ check (tempDir );
724+ }
725+
726+ void check (Path tempDir ) {
727+ assertThat (tempDirs .getLast ())//
728+ .isNotNull ()//
729+ .isSameAs (tempDir )//
730+ .isSameAs (this .tempDir );
731+ assertTrue (Files .exists (tempDir ));
705732 }
706733
707734 }
708735
736+ static class SeparateTempDirsConstructorInjectionPerMethodTestCase extends BaseConstructorInjectionTestCase {
737+
738+ SeparateTempDirsConstructorInjectionPerMethodTestCase (@ TempDir Path tempDir ) {
739+ super (tempDir );
740+ }
741+
742+ @ BeforeEach
743+ void beforeEach (@ TempDir Path tempDir ) {
744+ for (Path dir : tempDirs ) {
745+ assertThat (dir ).doesNotExist ();
746+ }
747+ assertThat (tempDirs ).doesNotContain (tempDir );
748+ tempDirs .add (tempDir );
749+ check (tempDir );
750+ }
751+ }
752+
709753 @ TestInstance (PER_CLASS )
710- static class AnnotationOnConstructorParameterWithTestInstancePerClassTestCase
711- extends AnnotationOnConstructorParameterTestCase {
754+ static class SharedTempDirsConstructorInjectionPerClassTestCase extends BaseConstructorInjectionTestCase {
712755
713- AnnotationOnConstructorParameterWithTestInstancePerClassTestCase (@ TempDir Path tempDir ) {
756+ SharedTempDirsConstructorInjectionPerClassTestCase (@ TempDir Path tempDir ) {
714757 super (tempDir );
715758 }
759+
760+ @ BeforeEach
761+ void beforeEach (@ TempDir Path tempDir ) {
762+ for (Path dir : tempDirs ) {
763+ assertThat (dir ).isSameAs (tempDir ).exists ();
764+ }
765+ tempDirs .add (tempDir );
766+ check (tempDir );
767+ }
716768 }
717769
718770 static class NonDefaultFactoryTestCase {
0 commit comments