@@ -98,7 +98,7 @@ void cleanupRoot() throws IOException {
98
98
@ Test
99
99
@ DisplayName ("succeeds if the factory returns a directory" )
100
100
void factoryReturnsDirectory () throws Exception {
101
- TempDirFactory factory = ( elementContext , extensionContext ) -> createDirectory (root .resolve ("directory" ));
101
+ TempDirFactory factory = spy ( new Factory ( createDirectory (root .resolve ("directory" )) ));
102
102
103
103
closeablePath = TempDirectory .createTempDir (factory , DEFAULT , elementContext , extensionContext );
104
104
assertThat (closeablePath .get ()).isDirectory ();
@@ -111,8 +111,7 @@ void factoryReturnsDirectory() throws Exception {
111
111
@ DisabledOnOs (OS .WINDOWS )
112
112
void factoryReturnsSymbolicLinkToDirectory () throws Exception {
113
113
Path directory = createDirectory (root .resolve ("directory" ));
114
- TempDirFactory factory = (elementContext ,
115
- extensionContext ) -> createSymbolicLink (root .resolve ("symbolicLink" ), directory );
114
+ TempDirFactory factory = spy (new Factory (createSymbolicLink (root .resolve ("symbolicLink" ), directory )));
116
115
117
116
closeablePath = TempDirectory .createTempDir (factory , DEFAULT , elementContext , extensionContext );
118
117
assertThat (closeablePath .get ()).isDirectory ();
@@ -123,23 +122,26 @@ void factoryReturnsSymbolicLinkToDirectory() throws Exception {
123
122
124
123
@ Test
125
124
@ DisplayName ("fails if the factory returns null" )
126
- void factoryReturnsNull () {
127
- TempDirFactory factory = ( elementContext , extensionContext ) -> null ;
125
+ void factoryReturnsNull () throws IOException {
126
+ TempDirFactory factory = spy ( new Factory ( null )) ;
128
127
129
128
assertThatExtensionConfigurationExceptionIsThrownBy (
130
129
() -> TempDirectory .createTempDir (factory , DEFAULT , elementContext , extensionContext ));
130
+
131
+ verify (factory ).close ();
131
132
}
132
133
133
134
@ Test
134
135
@ DisplayName ("fails if the factory returns a file" )
135
136
void factoryReturnsFile () throws IOException {
136
137
Path file = createFile (root .resolve ("file" ));
137
- TempDirFactory factory = ( elementContext , extensionContext ) -> file ;
138
+ TempDirFactory factory = spy ( new Factory ( file )) ;
138
139
139
140
assertThatExtensionConfigurationExceptionIsThrownBy (
140
141
() -> TempDirectory .createTempDir (factory , DEFAULT , elementContext , extensionContext ));
141
142
142
- delete (file );
143
+ verify (factory ).close ();
144
+ assertThat (file ).doesNotExist ();
143
145
}
144
146
145
147
@ Test
@@ -148,15 +150,27 @@ void factoryReturnsFile() throws IOException {
148
150
void factoryReturnsSymbolicLinkToFile () throws IOException {
149
151
Path file = createFile (root .resolve ("file" ));
150
152
Path symbolicLink = createSymbolicLink (root .resolve ("symbolicLink" ), file );
151
- TempDirFactory factory = ( elementContext , extensionContext ) -> symbolicLink ;
153
+ TempDirFactory factory = spy ( new Factory ( symbolicLink )) ;
152
154
153
155
assertThatExtensionConfigurationExceptionIsThrownBy (
154
156
() -> TempDirectory .createTempDir (factory , DEFAULT , elementContext , extensionContext ));
155
157
156
- delete (symbolicLink );
158
+ verify (factory ).close ();
159
+ assertThat (symbolicLink ).doesNotExist ();
160
+
157
161
delete (file );
158
162
}
159
163
164
+ // Mockito spying a lambda fails with: VM does not support modification of given type
165
+ private record Factory (Path path ) implements TempDirFactory {
166
+
167
+ @ Override
168
+ public Path createTempDirectory (AnnotatedElementContext elementContext , ExtensionContext extensionContext ) {
169
+ return path ;
170
+ }
171
+
172
+ }
173
+
160
174
private static void assertThatExtensionConfigurationExceptionIsThrownBy (ThrowingCallable callable ) {
161
175
assertThatExceptionOfType (ExtensionConfigurationException .class )//
162
176
.isThrownBy (callable )//
@@ -194,8 +208,9 @@ void always() throws IOException {
194
208
assertThat (closeablePath .get ()).isDirectory ();
195
209
196
210
closeablePath .close ();
197
- assertThat ( closeablePath . get ()). doesNotExist ();
211
+
198
212
verify (factory ).close ();
213
+ assertThat (closeablePath .get ()).doesNotExist ();
199
214
}
200
215
201
216
@ Test
@@ -205,8 +220,9 @@ void never() throws IOException {
205
220
assertThat (closeablePath .get ()).isDirectory ();
206
221
207
222
closeablePath .close ();
208
- assertThat ( closeablePath . get ()). exists ();
223
+
209
224
verify (factory ).close ();
225
+ assertThat (closeablePath .get ()).exists ();
210
226
}
211
227
212
228
@ Test
@@ -218,8 +234,9 @@ void onSuccessWithException() throws IOException {
218
234
assertThat (closeablePath .get ()).isDirectory ();
219
235
220
236
closeablePath .close ();
221
- assertThat ( closeablePath . get ()). exists ();
237
+
222
238
verify (factory ).close ();
239
+ assertThat (closeablePath .get ()).exists ();
223
240
}
224
241
225
242
@ Test
@@ -231,8 +248,9 @@ void onSuccessWithNoException() throws IOException {
231
248
assertThat (closeablePath .get ()).isDirectory ();
232
249
233
250
closeablePath .close ();
234
- assertThat ( closeablePath . get ()). doesNotExist ();
251
+
235
252
verify (factory ).close ();
253
+ assertThat (closeablePath .get ()).doesNotExist ();
236
254
}
237
255
238
256
}
0 commit comments