19
19
20
20
import java .io .FileNotFoundException ;
21
21
import java .lang .reflect .AnnotatedElement ;
22
+ import java .lang .reflect .Constructor ;
22
23
import java .lang .reflect .Method ;
23
24
import java .util .Arrays ;
24
25
import java .util .Map ;
40
41
import org .junit .jupiter .params .provider .ArgumentsSource ;
41
42
import org .junit .platform .commons .JUnitException ;
42
43
import org .junit .platform .commons .PreconditionViolationException ;
44
+ import org .junit .platform .commons .util .ReflectionUtils ;
43
45
import org .junit .platform .engine .support .store .NamespacedHierarchicalStore ;
44
46
45
47
/**
@@ -150,11 +152,14 @@ void throwsExceptionWhenArgumentsProviderIsNotStatic() {
150
152
151
153
var exception = assertThrows (JUnitException .class , stream ::toArray );
152
154
153
- assertArgumentsProviderInstantiationException (exception , NonStaticArgumentsProvider .class );
155
+ assertThat (exception ) //
156
+ .hasMessage (String .format (
157
+ "The ArgumentsProvider [%s] must be either a top-level class or a static nested class" ,
158
+ NonStaticArgumentsProvider .class .getName ()));
154
159
}
155
160
156
161
@ Test
157
- void throwsExceptionWhenArgumentsProviderDoesNotContainNoArgumentConstructor () {
162
+ void throwsExceptionWhenArgumentsProviderDoesNotContainUnambiguousConstructor () {
158
163
var extensionContextWithAnnotatedTestMethod = getExtensionContextReturningSingleMethod (
159
164
new MissingNoArgumentsConstructorArgumentsProviderTestCase ());
160
165
@@ -163,15 +168,11 @@ void throwsExceptionWhenArgumentsProviderDoesNotContainNoArgumentConstructor() {
163
168
164
169
var exception = assertThrows (JUnitException .class , stream ::toArray );
165
170
166
- assertArgumentsProviderInstantiationException (exception , MissingNoArgumentsConstructorArgumentsProvider .class );
167
- }
168
-
169
- private <T > void assertArgumentsProviderInstantiationException (JUnitException exception , Class <T > clazz ) {
170
- assertThat (exception ).hasMessage (
171
- String .format ("Failed to find a no-argument constructor for ArgumentsProvider [%s]. "
172
- + "Please ensure that a no-argument constructor exists and "
173
- + "that the class is either a top-level class or a static nested class" ,
174
- clazz .getName ()));
171
+ String className = AmbiguousConstructorArgumentsProvider .class .getName ();
172
+ assertThat (exception ) //
173
+ .hasMessage (String .format ("Failed to find constructor for ArgumentsProvider [%s]. "
174
+ + "Please ensure that a no-argument or a single constructor exists." ,
175
+ className ));
175
176
}
176
177
177
178
private ExtensionContext getExtensionContextReturningSingleMethod (Object testCase ) {
@@ -277,7 +278,17 @@ public ExecutionMode getExecutionMode() {
277
278
278
279
@ Override
279
280
public ExecutableInvoker getExecutableInvoker () {
280
- return null ;
281
+ return new ExecutableInvoker () {
282
+ @ Override
283
+ public Object invoke (Method method , Object target ) {
284
+ return null ;
285
+ }
286
+
287
+ @ Override
288
+ public <T > T invoke (Constructor <T > constructor , Object outerInstance ) {
289
+ return ReflectionUtils .newInstance (constructor );
290
+ }
291
+ };
281
292
}
282
293
};
283
294
}
@@ -334,30 +345,33 @@ public Stream<? extends Arguments> provideArguments(ExtensionContext context) {
334
345
static class MissingNoArgumentsConstructorArgumentsProviderTestCase {
335
346
336
347
@ ParameterizedTest
337
- @ ArgumentsSource (MissingNoArgumentsConstructorArgumentsProvider .class )
348
+ @ ArgumentsSource (AmbiguousConstructorArgumentsProvider .class )
338
349
void method () {
339
350
}
340
351
}
341
352
342
353
static class EmptyDisplayNameProviderTestCase {
343
354
344
355
@ ParameterizedTest (name = "" )
345
- @ ArgumentsSource (MissingNoArgumentsConstructorArgumentsProvider .class )
356
+ @ ArgumentsSource (AmbiguousConstructorArgumentsProvider .class )
346
357
void method () {
347
358
}
348
359
}
349
360
350
361
static class DefaultDisplayNameProviderTestCase {
351
362
352
363
@ ParameterizedTest
353
- @ ArgumentsSource (MissingNoArgumentsConstructorArgumentsProvider .class )
364
+ @ ArgumentsSource (AmbiguousConstructorArgumentsProvider .class )
354
365
void method () {
355
366
}
356
367
}
357
368
358
- static class MissingNoArgumentsConstructorArgumentsProvider implements ArgumentsProvider {
369
+ static class AmbiguousConstructorArgumentsProvider implements ArgumentsProvider {
370
+
371
+ AmbiguousConstructorArgumentsProvider (String parameter ) {
372
+ }
359
373
360
- MissingNoArgumentsConstructorArgumentsProvider ( String parameter ) {
374
+ AmbiguousConstructorArgumentsProvider ( int parameter ) {
361
375
}
362
376
363
377
@ Override
0 commit comments