2929import org .junit .jupiter .api .AfterEach ;
3030import org .junit .jupiter .api .BeforeAll ;
3131import org .junit .jupiter .api .BeforeEach ;
32- import org .junit .jupiter .api .TestInstance ;
32+ import org .junit .jupiter .api .TestInstance . Lifecycle ;
3333import org .junit .jupiter .api .extension .AfterAllCallback ;
3434import org .junit .jupiter .api .extension .AfterEachCallback ;
3535import org .junit .jupiter .api .extension .AfterTestExecutionCallback ;
4242import org .junit .jupiter .api .extension .ParameterContext ;
4343import org .junit .jupiter .api .extension .ParameterResolver ;
4444import org .junit .jupiter .api .extension .TestInstancePostProcessor ;
45- import org .junit .jupiter .api .parallel .Execution ;
4645import org .junit .jupiter .api .parallel .ExecutionMode ;
4746import org .junit .platform .commons .annotation .Testable ;
4847
@@ -98,7 +97,7 @@ public class SpringExtension implements BeforeAllCallback, AfterAllCallback, Tes
9897 private static final Namespace AUTOWIRED_VALIDATION_NAMESPACE =
9998 Namespace .create (SpringExtension .class .getName () + "#autowired.validation" );
10099
101- private static final String NO_AUTOWIRED_VIOLATIONS_DETECTED = "NO AUTOWIRED VIOLATIONS DETECTED" ;
100+ private static final String NO_VIOLATIONS_DETECTED = "NO VIOLATIONS DETECTED" ;
102101
103102 /**
104103 * {@link Namespace} in which {@code @RecordApplicationEvents} validation error messages
@@ -153,8 +152,8 @@ public void postProcessTestInstance(Object testInstance, ExtensionContext contex
153152 }
154153
155154 /**
156- * Validate that test class or its enclosing class doesn't attempt to record
157- * application events in a parallel mode that makes it un -deterministic
155+ * Validate that the test class or its enclosing class doesn't attempt to record
156+ * application events in a parallel mode that makes it non -deterministic
158157 * ({@code @TestInstance(PER_CLASS)} and {@code @Execution(CONCURRENT)}
159158 * combination).
160159 * @since 6.1.0
@@ -165,30 +164,28 @@ private void validateRecordApplicationEventsConfig(ExtensionContext context) {
165164 Store store = context .getStore (RECORD_APPLICATION_EVENTS_VALIDATION_NAMESPACE );
166165
167166 String errorMessage = store .getOrComputeIfAbsent (context .getRequiredTestClass (), testClass -> {
168- boolean record = TestContextAnnotationUtils .hasAnnotation (testClass , RecordApplicationEvents .class );
169- if (!record ) {
170- return NO_AUTOWIRED_VIOLATIONS_DETECTED ;
167+ boolean recording = TestContextAnnotationUtils .hasAnnotation (testClass , RecordApplicationEvents .class );
168+ if (!recording ) {
169+ return NO_VIOLATIONS_DETECTED ;
171170 }
172- final TestInstance testInstance = TestContextAnnotationUtils .findMergedAnnotation (testClass , TestInstance .class );
173171
174- if (testInstance == null || testInstance . value () != TestInstance . Lifecycle .PER_CLASS ) {
175- return NO_AUTOWIRED_VIOLATIONS_DETECTED ;
172+ if (context . getTestInstanceLifecycle (). orElse ( Lifecycle . PER_METHOD ) == Lifecycle .PER_METHOD ) {
173+ return NO_VIOLATIONS_DETECTED ;
176174 }
177175
178- final Execution execution = TestContextAnnotationUtils .findMergedAnnotation (testClass , Execution .class );
179-
180- if (execution == null || execution .value () != ExecutionMode .CONCURRENT ) {
181- return NO_AUTOWIRED_VIOLATIONS_DETECTED ;
176+ if (context .getExecutionMode () == ExecutionMode .SAME_THREAD ) {
177+ return NO_VIOLATIONS_DETECTED ;
182178 }
183179
184- return "Test classes or inner classes that @RecordApplicationEvents must not be run in parallel "
185- + "with the @TestInstance(Lifecycle.PER_CLASS) configuration. Use either @Execution(SAME_THREAD), "
186- + "@TestInstance(PER_METHOD) or disable parallel execution altogether. Note that when recording "
187- + "events in parallel, one might see events published by other tests as the application context "
188- + "can be common." ;
180+ return """
181+ Test classes or @Nested test classes that @RecordApplicationEvents must not be run \
182+ in parallel with the @TestInstance(PER_CLASS) lifecycle mode. Configure either \
183+ @Execution(SAME_THREAD) or @TestInstance(PER_METHOD) semantics, or disable parallel \
184+ execution altogether. Note that when recording events in parallel, one might see events \
185+ published by other tests since the application context may be shared.""" ;
189186 }, String .class );
190187
191- if (errorMessage != NO_AUTOWIRED_VIOLATIONS_DETECTED ) {
188+ if (errorMessage != NO_VIOLATIONS_DETECTED ) {
192189 throw new IllegalStateException (errorMessage );
193190 }
194191 }
@@ -206,15 +203,15 @@ private void validateAutowiredConfig(ExtensionContext context) {
206203 String errorMessage = store .getOrComputeIfAbsent (context .getRequiredTestClass (), testClass -> {
207204 Method [] methodsWithErrors =
208205 ReflectionUtils .getUniqueDeclaredMethods (testClass , autowiredTestOrLifecycleMethodFilter );
209- return (methodsWithErrors .length == 0 ? NO_AUTOWIRED_VIOLATIONS_DETECTED :
206+ return (methodsWithErrors .length == 0 ? NO_VIOLATIONS_DETECTED :
210207 String .format (
211208 "Test methods and test lifecycle methods must not be annotated with @Autowired. " +
212209 "You should instead annotate individual method parameters with @Autowired, " +
213210 "@Qualifier, or @Value. Offending methods in test class %s: %s" ,
214211 testClass .getName (), Arrays .toString (methodsWithErrors )));
215212 }, String .class );
216213
217- if (errorMessage != NO_AUTOWIRED_VIOLATIONS_DETECTED ) {
214+ if (errorMessage != NO_VIOLATIONS_DETECTED ) {
218215 throw new IllegalStateException (errorMessage );
219216 }
220217 }
0 commit comments