18
18
import org .junit .jupiter .api .extension .ExtensionContext ;
19
19
import org .junit .platform .commons .JUnitException ;
20
20
import org .junit .platform .commons .PreconditionViolationException ;
21
- import org .junit .platform .commons .support . ModifierSupport ;
21
+ import org .junit .platform .commons .util . Preconditions ;
22
22
import org .junit .platform .commons .util .ReflectionUtils ;
23
23
24
24
/**
25
25
* @since 5.12
26
26
*/
27
27
class ParameterizedTestSpiInstantiator {
28
28
29
- static <T > T instantiate (Class <T > spiClass , Class <? extends T > clazz , ExtensionContext extensionContext ) {
30
- return extensionContext .getExecutableInvoker ().invoke (findConstructor (spiClass , clazz ));
29
+ static <T > T instantiate (Class <T > spiInterface , Class <? extends T > implementationClass ,
30
+ ExtensionContext extensionContext ) {
31
+ return extensionContext .getExecutableInvoker () //
32
+ .invoke (findConstructor (spiInterface , implementationClass ));
31
33
}
32
34
33
35
/**
@@ -37,26 +39,34 @@ static <T> T instantiate(Class<T> spiClass, Class<? extends T> clazz, ExtensionC
37
39
* which takes precedence over any other constructor. If no default
38
40
* constructor is found, it checks for a single constructor and returns it.
39
41
*/
42
+ private static <T , V extends T > Constructor <? extends V > findConstructor (Class <T > spiInterface ,
43
+ Class <V > implementationClass ) {
44
+
45
+ Preconditions .condition (!ReflectionUtils .isInnerClass (implementationClass ),
46
+ () -> String .format ("The %s [%s] must be either a top-level class or a static nested class" ,
47
+ spiInterface .getSimpleName (), implementationClass .getName ()));
48
+
49
+ return findDefaultConstructor (implementationClass ) //
50
+ .orElseGet (() -> findSingleConstructor (spiInterface , implementationClass ));
51
+ }
52
+
40
53
@ SuppressWarnings ("unchecked" )
41
- private static <T > Constructor <? extends T > findConstructor (Class <T > spiClass , Class <? extends T > clazz ) {
42
- Optional <Constructor <?>> defaultConstructor = getFirstElement (
43
- ReflectionUtils .findConstructors (clazz , it -> it .getParameterCount () == 0 ));
44
- if (defaultConstructor .isPresent ()) {
45
- return (Constructor <? extends T >) defaultConstructor .get ();
46
- }
47
- if (ModifierSupport .isNotStatic (clazz )) {
48
- String message = String .format ("The %s [%s] must be either a top-level class or a static nested class" ,
49
- spiClass .getSimpleName (), clazz .getName ());
50
- throw new JUnitException (message );
51
- }
54
+ private static <T > Optional <Constructor <T >> findDefaultConstructor (Class <T > clazz ) {
55
+ return getFirstElement (ReflectionUtils .findConstructors (clazz , it -> it .getParameterCount () == 0 )) //
56
+ .map (it -> (Constructor <T >) it );
57
+ }
58
+
59
+ private static <T , V extends T > Constructor <V > findSingleConstructor (Class <T > spiInterface ,
60
+ Class <V > implementationClass ) {
61
+
52
62
try {
53
- return ReflectionUtils .getDeclaredConstructor (clazz );
63
+ return ReflectionUtils .getDeclaredConstructor (implementationClass );
54
64
}
55
65
catch (PreconditionViolationException ex ) {
56
66
String message = String .format (
57
67
"Failed to find constructor for %s [%s]. "
58
68
+ "Please ensure that a no-argument or a single constructor exists." ,
59
- spiClass .getSimpleName (), clazz .getName ());
69
+ spiInterface .getSimpleName (), implementationClass .getName ());
60
70
throw new JUnitException (message );
61
71
}
62
72
}
0 commit comments