1010import java .util .Set ;
1111import java .util .concurrent .ConcurrentHashMap ;
1212import java .util .concurrent .atomic .AtomicReference ;
13+ import java .util .function .Predicate ;
1314import java .util .regex .Pattern ;
1415import java .util .stream .Collectors ;
1516import java .util .stream .Stream ;
@@ -90,6 +91,40 @@ protected static ITestNGMethod[] findDependedUponMethods(
9091 return findDependedUponMethods (m , methodsArray );
9192 }
9293
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+
93128 /**
94129 * Finds TestNG methods that the specified TestNG method depends upon
95130 *
@@ -104,6 +139,25 @@ public static ITestNGMethod[] findDependedUponMethods(ITestNGMethod m, ITestNGMe
104139 .filter (each -> Objects .isNull (each .getRealClass ().getEnclosingClass ()))
105140 .toArray (ITestNGMethod []::new );
106141 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+ }
107161
108162 List <ITestNGMethod > vResult = Lists .newArrayList ();
109163 String regexp = null ;
@@ -142,11 +196,17 @@ public static ITestNGMethod[] findDependedUponMethods(ITestNGMethod m, ITestNGMe
142196 }
143197 Method maybeReferringTo = findMethodByName (m , regexp );
144198 if (maybeReferringTo != null ) {
199+ String suffix = " or not included." ;
200+ if (isConfigurationMethod (m )) {
201+ suffix = "." ;
202+ }
145203 throw new TestNGException (
146204 canonicalMethodName
147205 + "() is depending on method "
148206 + maybeReferringTo
149- + ", which is not annotated with @Test or not included." );
207+ + ", which is not annotated with @"
208+ + annotationType
209+ + suffix );
150210 }
151211 throw new TestNGException (
152212 canonicalMethodName + "() depends on nonexistent method " + regexp );
0 commit comments