10
10
import java .util .Set ;
11
11
import java .util .concurrent .ConcurrentHashMap ;
12
12
import java .util .concurrent .atomic .AtomicReference ;
13
+ import java .util .function .Predicate ;
13
14
import java .util .regex .Pattern ;
14
15
import java .util .stream .Collectors ;
15
16
import java .util .stream .Stream ;
@@ -90,6 +91,40 @@ protected static ITestNGMethod[] findDependedUponMethods(
90
91
return findDependedUponMethods (m , methodsArray );
91
92
}
92
93
94
+ private static Pair <String , Predicate <ITestNGMethod >> filterToUse (ITestNGMethod m ) {
95
+ if (m .isBeforeMethodConfiguration ()) {
96
+ return new Pair <>("BeforeMethod" , ITestNGMethod ::isBeforeMethodConfiguration );
97
+ }
98
+ if (m .isAfterMethodConfiguration ()) {
99
+ return new Pair <>("AfterMethod" , ITestNGMethod ::isAfterMethodConfiguration );
100
+ }
101
+ if (m .isBeforeClassConfiguration ()) {
102
+ return new Pair <>("BeforeClass" , ITestNGMethod ::isBeforeClassConfiguration );
103
+ }
104
+ if (m .isAfterClassConfiguration ()) {
105
+ return new Pair <>("AfterClass" , ITestNGMethod ::isAfterClassConfiguration );
106
+ }
107
+ if (m .isBeforeTestConfiguration ()) {
108
+ return new Pair <>("BeforeTest" , ITestNGMethod ::isBeforeTestConfiguration );
109
+ }
110
+ if (m .isAfterTestConfiguration ()) {
111
+ return new Pair <>("AfterTest" , ITestNGMethod ::isAfterTestConfiguration );
112
+ }
113
+ if (m .isBeforeSuiteConfiguration ()) {
114
+ return new Pair <>("BeforeSuite" , ITestNGMethod ::isBeforeSuiteConfiguration );
115
+ }
116
+ if (m .isAfterSuiteConfiguration ()) {
117
+ return new Pair <>("AfterSuite" , ITestNGMethod ::isAfterSuiteConfiguration );
118
+ }
119
+ if (m .isBeforeGroupsConfiguration ()) {
120
+ return new Pair <>("BeforeGroups" , ITestNGMethod ::isBeforeGroupsConfiguration );
121
+ }
122
+ if (m .isAfterGroupsConfiguration ()) {
123
+ return new Pair <>("AfterGroups" , ITestNGMethod ::isAfterGroupsConfiguration );
124
+ }
125
+ return new Pair <>("Test" , ITestNGMethod ::isTest );
126
+ }
127
+
93
128
/**
94
129
* Finds TestNG methods that the specified TestNG method depends upon
95
130
*
@@ -104,6 +139,25 @@ public static ITestNGMethod[] findDependedUponMethods(ITestNGMethod m, ITestNGMe
104
139
.filter (each -> Objects .isNull (each .getRealClass ().getEnclosingClass ()))
105
140
.toArray (ITestNGMethod []::new );
106
141
String canonicalMethodName = calculateMethodCanonicalName (m );
142
+ Pair <String , Predicate <ITestNGMethod >> filterPair = filterToUse (m );
143
+ String annotationType = filterPair .first ();
144
+ Predicate <ITestNGMethod > predicate = filterPair .second ();
145
+
146
+ if (isConfigurationMethod (m )) {
147
+ methods =
148
+ Arrays .stream (incoming )
149
+ .filter (tm -> !tm .equals (m )) // exclude the current config method from the list
150
+ .filter (predicate ) // include only similar config methods
151
+ .toArray (ITestNGMethod []::new );
152
+
153
+ if (methods .length == 0 ) {
154
+ String msg =
155
+ String .format (
156
+ "None of the dependencies of the method %s are annotated with [@%s]." ,
157
+ canonicalMethodName , annotationType );
158
+ throw new TestNGException (msg );
159
+ }
160
+ }
107
161
108
162
List <ITestNGMethod > vResult = Lists .newArrayList ();
109
163
String regexp = null ;
@@ -142,11 +196,17 @@ public static ITestNGMethod[] findDependedUponMethods(ITestNGMethod m, ITestNGMe
142
196
}
143
197
Method maybeReferringTo = findMethodByName (m , regexp );
144
198
if (maybeReferringTo != null ) {
199
+ String suffix = " or not included." ;
200
+ if (isConfigurationMethod (m )) {
201
+ suffix = "." ;
202
+ }
145
203
throw new TestNGException (
146
204
canonicalMethodName
147
205
+ "() is depending on method "
148
206
+ maybeReferringTo
149
- + ", which is not annotated with @Test or not included." );
207
+ + ", which is not annotated with @"
208
+ + annotationType
209
+ + suffix );
150
210
}
151
211
throw new TestNGException (
152
212
canonicalMethodName + "() depends on nonexistent method " + regexp );
0 commit comments