29
29
import org .junit .jupiter .api .AfterEach ;
30
30
import org .junit .jupiter .api .BeforeAll ;
31
31
import org .junit .jupiter .api .BeforeEach ;
32
- import org .junit .jupiter .api .TestInstance ;
32
+ import org .junit .jupiter .api .TestInstance . Lifecycle ;
33
33
import org .junit .jupiter .api .extension .AfterAllCallback ;
34
34
import org .junit .jupiter .api .extension .AfterEachCallback ;
35
35
import org .junit .jupiter .api .extension .AfterTestExecutionCallback ;
42
42
import org .junit .jupiter .api .extension .ParameterContext ;
43
43
import org .junit .jupiter .api .extension .ParameterResolver ;
44
44
import org .junit .jupiter .api .extension .TestInstancePostProcessor ;
45
- import org .junit .jupiter .api .parallel .Execution ;
46
45
import org .junit .jupiter .api .parallel .ExecutionMode ;
47
46
import org .junit .platform .commons .annotation .Testable ;
48
47
@@ -98,7 +97,7 @@ public class SpringExtension implements BeforeAllCallback, AfterAllCallback, Tes
98
97
private static final Namespace AUTOWIRED_VALIDATION_NAMESPACE =
99
98
Namespace .create (SpringExtension .class .getName () + "#autowired.validation" );
100
99
101
- private static final String NO_AUTOWIRED_VIOLATIONS_DETECTED = "NO AUTOWIRED VIOLATIONS DETECTED" ;
100
+ private static final String NO_VIOLATIONS_DETECTED = "NO VIOLATIONS DETECTED" ;
102
101
103
102
/**
104
103
* {@link Namespace} in which {@code @RecordApplicationEvents} validation error messages
@@ -153,8 +152,8 @@ public void postProcessTestInstance(Object testInstance, ExtensionContext contex
153
152
}
154
153
155
154
/**
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
158
157
* ({@code @TestInstance(PER_CLASS)} and {@code @Execution(CONCURRENT)}
159
158
* combination).
160
159
* @since 6.1.0
@@ -165,30 +164,28 @@ private void validateRecordApplicationEventsConfig(ExtensionContext context) {
165
164
Store store = context .getStore (RECORD_APPLICATION_EVENTS_VALIDATION_NAMESPACE );
166
165
167
166
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 ;
171
170
}
172
- final TestInstance testInstance = TestContextAnnotationUtils .findMergedAnnotation (testClass , TestInstance .class );
173
171
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 ;
176
174
}
177
175
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 ;
182
178
}
183
179
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.""" ;
189
186
}, String .class );
190
187
191
- if (errorMessage != NO_AUTOWIRED_VIOLATIONS_DETECTED ) {
188
+ if (errorMessage != NO_VIOLATIONS_DETECTED ) {
192
189
throw new IllegalStateException (errorMessage );
193
190
}
194
191
}
@@ -206,15 +203,15 @@ private void validateAutowiredConfig(ExtensionContext context) {
206
203
String errorMessage = store .getOrComputeIfAbsent (context .getRequiredTestClass (), testClass -> {
207
204
Method [] methodsWithErrors =
208
205
ReflectionUtils .getUniqueDeclaredMethods (testClass , autowiredTestOrLifecycleMethodFilter );
209
- return (methodsWithErrors .length == 0 ? NO_AUTOWIRED_VIOLATIONS_DETECTED :
206
+ return (methodsWithErrors .length == 0 ? NO_VIOLATIONS_DETECTED :
210
207
String .format (
211
208
"Test methods and test lifecycle methods must not be annotated with @Autowired. " +
212
209
"You should instead annotate individual method parameters with @Autowired, " +
213
210
"@Qualifier, or @Value. Offending methods in test class %s: %s" ,
214
211
testClass .getName (), Arrays .toString (methodsWithErrors )));
215
212
}, String .class );
216
213
217
- if (errorMessage != NO_AUTOWIRED_VIOLATIONS_DETECTED ) {
214
+ if (errorMessage != NO_VIOLATIONS_DETECTED ) {
218
215
throw new IllegalStateException (errorMessage );
219
216
}
220
217
}
0 commit comments