@@ -52,25 +52,25 @@ public class ModifiedClassPathExtension implements InvocationInterceptor {
52
52
@ Override
53
53
public void interceptBeforeAllMethod (Invocation <Void > invocation ,
54
54
ReflectiveInvocationContext <Method > invocationContext , ExtensionContext extensionContext ) throws Throwable {
55
- interceptInvocation (invocation , extensionContext );
55
+ intercept (invocation , extensionContext );
56
56
}
57
57
58
58
@ Override
59
59
public void interceptBeforeEachMethod (Invocation <Void > invocation ,
60
60
ReflectiveInvocationContext <Method > invocationContext , ExtensionContext extensionContext ) throws Throwable {
61
- interceptInvocation (invocation , extensionContext );
61
+ intercept (invocation , extensionContext );
62
62
}
63
63
64
64
@ Override
65
65
public void interceptAfterEachMethod (Invocation <Void > invocation ,
66
66
ReflectiveInvocationContext <Method > invocationContext , ExtensionContext extensionContext ) throws Throwable {
67
- interceptInvocation (invocation , extensionContext );
67
+ intercept (invocation , extensionContext );
68
68
}
69
69
70
70
@ Override
71
71
public void interceptAfterAllMethod (Invocation <Void > invocation ,
72
72
ReflectiveInvocationContext <Method > invocationContext , ExtensionContext extensionContext ) throws Throwable {
73
- interceptInvocation (invocation , extensionContext );
73
+ intercept (invocation , extensionContext );
74
74
}
75
75
76
76
@ Override
@@ -80,62 +80,64 @@ public void interceptTestMethod(Invocation<Void> invocation, ReflectiveInvocatio
80
80
invocation .proceed ();
81
81
return ;
82
82
}
83
+ fakeInvocation (invocation );
84
+ runTestWithModifiedClassPath (invocationContext , extensionContext );
85
+ }
86
+
87
+ private void runTestWithModifiedClassPath (ReflectiveInvocationContext <Method > invocationContext ,
88
+ ExtensionContext extensionContext ) throws ClassNotFoundException , Throwable {
89
+ Class <?> testClass = extensionContext .getRequiredTestClass ();
90
+ Method testMethod = invocationContext .getExecutable ();
83
91
ClassLoader originalClassLoader = Thread .currentThread ().getContextClassLoader ();
84
- URLClassLoader classLoader = ModifiedClassPathClassLoaderFactory
85
- .createTestClassLoader (extensionContext .getRequiredTestClass ());
86
- Thread .currentThread ().setContextClassLoader (classLoader );
92
+ URLClassLoader modifiedClassLoader = ModifiedClassPathClassLoaderFactory .createTestClassLoader (testClass );
93
+ Thread .currentThread ().setContextClassLoader (modifiedClassLoader );
87
94
try {
88
- fakeInvocation (invocation );
89
- TestExecutionSummary summary = launchTests (invocationContext , extensionContext , classLoader );
90
- if (!CollectionUtils .isEmpty (summary .getFailures ())) {
91
- throw summary .getFailures ().get (0 ).getException ();
92
- }
93
- }
94
- catch (Exception ex ) {
95
- throw ex ;
95
+ runTest (modifiedClassLoader , testClass .getName (), testMethod .getName ());
96
96
}
97
97
finally {
98
98
Thread .currentThread ().setContextClassLoader (originalClassLoader );
99
99
}
100
100
}
101
101
102
- private TestExecutionSummary launchTests ( ReflectiveInvocationContext < Method > invocationContext ,
103
- ExtensionContext extensionContext , URLClassLoader classLoader ) throws ClassNotFoundException {
104
- Class <?> testClass = classLoader .loadClass (extensionContext . getRequiredTestClass (). getName () );
105
- Method method = ReflectionUtils .findMethod (testClass , invocationContext . getExecutable (). getName () );
102
+ private void runTest ( URLClassLoader classLoader , String testClassName , String testMethodName )
103
+ throws ClassNotFoundException , Throwable {
104
+ Class <?> testClass = classLoader .loadClass (testClassName );
105
+ Method testMethod = ReflectionUtils .findMethod (testClass , testMethodName );
106
106
LauncherDiscoveryRequest request = LauncherDiscoveryRequestBuilder .request ()
107
- .selectors (DiscoverySelectors .selectMethod (testClass , method )).build ();
107
+ .selectors (DiscoverySelectors .selectMethod (testClass , testMethod )).build ();
108
108
Launcher launcher = LauncherFactory .create ();
109
109
TestPlan testPlan = launcher .discover (request );
110
110
SummaryGeneratingListener listener = new SummaryGeneratingListener ();
111
111
launcher .registerTestExecutionListeners (listener );
112
112
launcher .execute (testPlan );
113
- return listener .getSummary ();
114
- }
115
-
116
- private boolean isModifiedClassPathClassLoader (ExtensionContext extensionContext ) {
117
- return extensionContext .getRequiredTestClass ().getClassLoader ().getClass ().getName ()
118
- .equals (ModifiedClassPathClassLoader .class .getName ());
113
+ TestExecutionSummary summary = listener .getSummary ();
114
+ if (!CollectionUtils .isEmpty (summary .getFailures ())) {
115
+ throw summary .getFailures ().get (0 ).getException ();
116
+ }
119
117
}
120
118
121
- private void interceptInvocation (Invocation <Void > invocation , ExtensionContext extensionContext ) throws Throwable {
119
+ private void intercept (Invocation <Void > invocation , ExtensionContext extensionContext ) throws Throwable {
122
120
if (isModifiedClassPathClassLoader (extensionContext )) {
123
121
invocation .proceed ();
122
+ return ;
124
123
}
125
- else {
126
- fakeInvocation (invocation );
127
- }
124
+ fakeInvocation (invocation );
128
125
}
129
126
130
- private void fakeInvocation (Invocation invocation ) {
127
+ private void fakeInvocation (Invocation < Void > invocation ) {
131
128
try {
132
129
Field field = ReflectionUtils .findField (invocation .getClass (), "invoked" );
133
130
ReflectionUtils .makeAccessible (field );
134
131
ReflectionUtils .setField (field , invocation , new AtomicBoolean (true ));
135
132
}
136
- catch (Throwable ignore ) {
137
-
133
+ catch (Throwable ex ) {
138
134
}
139
135
}
140
136
137
+ private boolean isModifiedClassPathClassLoader (ExtensionContext extensionContext ) {
138
+ Class <?> testClass = extensionContext .getRequiredTestClass ();
139
+ ClassLoader classLoader = testClass .getClassLoader ();
140
+ return classLoader .getClass ().getName ().equals (ModifiedClassPathClassLoader .class .getName ());
141
+ }
142
+
141
143
}
0 commit comments