@@ -182,7 +182,7 @@ public void afterTestClassAnnotation() throws Exception {
182
182
}
183
183
184
184
@ RunWith (SpringRunner .class )
185
- @ ContextConfiguration (classes = EventCaptureConfiguration .class )
185
+ @ ContextConfiguration (classes = TestEventListenerConfiguration .class )
186
186
@ TestExecutionListeners (listeners = EventPublishingTestExecutionListener .class , mergeMode = MERGE_WITH_DEFAULTS )
187
187
public static class ExampleTestCase {
188
188
@@ -211,7 +211,7 @@ public void testWithFailingAsyncEventListener() {
211
211
212
212
@ Configuration
213
213
@ EnableAsync (proxyTargetClass = true )
214
- static class EventCaptureConfiguration extends AsyncConfigurerSupport {
214
+ static class TestEventListenerConfiguration extends AsyncConfigurerSupport {
215
215
216
216
@ Override
217
217
public Executor getAsyncExecutor () {
@@ -231,88 +231,97 @@ public TestExecutionListener listener() {
231
231
return mock (TestExecutionListener .class );
232
232
}
233
233
234
+ /**
235
+ * The {@code @Async} test event listener method must reside in a separate
236
+ * component since {@code @Async} is not supported on methods in
237
+ * {@code @Configuration} classes.
238
+ */
234
239
@ Bean
235
- EventCaptureBean eventCaptureBean () {
236
- return new EventCaptureBean (listener ());
237
- }
238
-
239
- }
240
-
241
- static class TrackingAsyncUncaughtExceptionHandler implements AsyncUncaughtExceptionHandler {
242
-
243
- static volatile Throwable asyncException ;
244
-
245
-
246
- @ Override
247
- public void handleUncaughtException (Throwable exception , Method method , Object ... params ) {
248
- asyncException = exception ;
249
- countDownLatch .countDown ();
250
- }
251
- }
252
-
253
- // MUST be annotated with @Component due to a change in Spring 5.1 that
254
- // does not consider beans in a package starting with "org.springframework"
255
- // to be event listeners unless they are also components. See
256
- // org.springframework.context.event.EventListenerMethodProcessor.isSpringContainerClass(Class<?>)
257
- // for details.
258
- @ Component
259
- static class EventCaptureBean {
260
-
261
- final TestExecutionListener listener ;
262
-
263
-
264
- EventCaptureBean (TestExecutionListener listener ) {
265
- this .listener = listener ;
240
+ AsyncTestEventComponent asyncTestEventComponent () {
241
+ return new AsyncTestEventComponent (listener ());
266
242
}
267
243
268
244
@ BeforeTestClass ("#root.event.source.testClass.name matches '.+TestCase'" )
269
245
public void beforeTestClass (BeforeTestClassEvent e ) throws Exception {
270
- this . listener .beforeTestClass (e .getSource ());
246
+ listener () .beforeTestClass (e .getSource ());
271
247
}
272
248
273
249
@ PrepareTestInstance ("#a0.testContext.testClass.name matches '.+TestCase'" )
274
250
public void prepareTestInstance (PrepareTestInstanceEvent e ) throws Exception {
275
- this . listener .prepareTestInstance (e .getSource ());
251
+ listener () .prepareTestInstance (e .getSource ());
276
252
}
277
253
278
254
@ BeforeTestMethod ("#p0.testContext.testMethod.isAnnotationPresent(T(org.springframework.test.context.event.EventPublishingTestExecutionListenerIntegrationTests.Traceable))" )
279
255
public void beforeTestMethod (BeforeTestMethodEvent e ) throws Exception {
280
- this . listener .beforeTestMethod (e .getSource ());
256
+ listener () .beforeTestMethod (e .getSource ());
281
257
}
282
258
283
259
@ BeforeTestMethod ("event.testContext.testMethod.name == 'testWithFailingEventListener'" )
284
260
public void beforeTestMethodWithFailure (BeforeTestMethodEvent event ) throws Exception {
285
- this . listener .beforeTestMethod (event .getSource ());
261
+ listener () .beforeTestMethod (event .getSource ());
286
262
throw new RuntimeException ("Boom!" );
287
263
}
288
264
289
- @ BeforeTestMethod ("event.testContext.testMethod.name == 'testWithFailingAsyncEventListener'" )
290
- @ Async
291
- public void beforeTestMethodWithAsyncFailure (BeforeTestMethodEvent event ) throws Exception {
292
- this .listener .beforeTestMethod (event .getSource ());
293
- throw new RuntimeException (String .format ("Asynchronous exception for test method [%s] in thread [%s]" ,
294
- event .getTestContext ().getTestMethod ().getName (), Thread .currentThread ().getName ()));
295
- }
296
-
297
265
@ BeforeTestExecution
298
266
public void beforeTestExecution (BeforeTestExecutionEvent e ) throws Exception {
299
- this . listener .beforeTestExecution (e .getSource ());
267
+ listener () .beforeTestExecution (e .getSource ());
300
268
}
301
269
302
270
@ AfterTestExecution
303
271
public void afterTestExecution (AfterTestExecutionEvent e ) throws Exception {
304
- this . listener .afterTestExecution (e .getSource ());
272
+ listener () .afterTestExecution (e .getSource ());
305
273
}
306
274
307
275
@ AfterTestMethod ("event.testContext.testMethod.isAnnotationPresent(T(org.springframework.test.context.event.EventPublishingTestExecutionListenerIntegrationTests.Traceable))" )
308
276
public void afterTestMethod (AfterTestMethodEvent e ) throws Exception {
309
- this . listener .afterTestMethod (e .getSource ());
277
+ listener () .afterTestMethod (e .getSource ());
310
278
}
311
279
312
280
@ AfterTestClass ("#afterTestClassEvent.testContext.testClass.name matches '.+TestCase'" )
313
281
public void afterTestClass (AfterTestClassEvent afterTestClassEvent ) throws Exception {
314
- this . listener .afterTestClass (afterTestClassEvent .getSource ());
282
+ listener () .afterTestClass (afterTestClassEvent .getSource ());
315
283
}
284
+
285
+ }
286
+
287
+ /**
288
+ * MUST be annotated with {@code @Component} due to a change in Spring 5.1 that
289
+ * does not consider beans in a package starting with "org.springframework" to be
290
+ * event listeners unless they are also components.
291
+ *
292
+ * @see org.springframework.context.event.EventListenerMethodProcessor#isSpringContainerClass
293
+ */
294
+ @ Component
295
+ static class AsyncTestEventComponent {
296
+
297
+ final TestExecutionListener listener ;
298
+
299
+
300
+ AsyncTestEventComponent (TestExecutionListener listener ) {
301
+ this .listener = listener ;
302
+ }
303
+
304
+ @ BeforeTestMethod ("event.testContext.testMethod.name == 'testWithFailingAsyncEventListener'" )
305
+ @ Async
306
+ public void beforeTestMethodWithAsyncFailure (BeforeTestMethodEvent event ) throws Exception {
307
+ this .listener .beforeTestMethod (event .getSource ());
308
+ throw new RuntimeException (String .format ("Asynchronous exception for test method [%s] in thread [%s]" ,
309
+ event .getTestContext ().getTestMethod ().getName (), Thread .currentThread ().getName ()));
310
+ }
311
+
312
+ }
313
+
314
+ static class TrackingAsyncUncaughtExceptionHandler implements AsyncUncaughtExceptionHandler {
315
+
316
+ static volatile Throwable asyncException ;
317
+
318
+
319
+ @ Override
320
+ public void handleUncaughtException (Throwable exception , Method method , Object ... params ) {
321
+ asyncException = exception ;
322
+ countDownLatch .countDown ();
323
+ }
324
+
316
325
}
317
326
318
327
}
0 commit comments