ImethodInterceptor not respected when using testgroup only

29 views
Skip to first unread message

DD

unread,
Mar 5, 2018, 5:18:48 PM3/5/18
to testng-users
Hi, 

I have an IMethodInterceptor which re-arranges the test methods based on priority of the class and the methods. 

It works fine when I specify which testclasses it has to run. But when I only specify which test group it has to run, the new sequence is not respected. 

Anyone any idee what can be the cause of this ? 

public class PriorityInterceptor implements IMethodInterceptor {

    @Override
    public List<IMethodInstance> intercept(List<IMethodInstance> methods,
                                           ITestContext context) {
        Comparator<IMethodInstance> comparator = new Comparator<IMethodInstance>() {

            private int getPriority(IMethodInstance mi) {
                int result = 0;
                int methodPrio = 0;
                int classPrio = 0;
               //Method method = mi.getMethod().getMethod();
                Method method = mi.getMethod().getConstructorOrMethod().getMethod();
                System.out.println("Testsd" + mi + "en" + method);
                Priority methodPriority = method.getAnnotation(Priority.class);
                System.out.println("Testsd" + methodPriority);
                Class<?> cls = method.getDeclaringClass();
                Priority classPriority = cls.getAnnotation(Priority.class);


                if (methodPriority != null) {
                    methodPrio = methodPriority.value();
                    classPrio = classPriority.value();

                    result = Integer.valueOf(String.valueOf(classPrio) + String.valueOf(methodPrio));
                    System.out.println("IfResult" + result);

                } else {

                    if (classPriority != null) {
                        classPrio = classPriority.value();
                        result = Integer.valueOf(String.valueOf(classPrio) + "0");
                        System.out.println("ElseResult" + result);
                    }
                }
                return result;
            }

            public int compare(IMethodInstance m1, IMethodInstance m2) {
                return getPriority(m1) - getPriority(m2);
            }

        };
        System.out.println(methods);
        IMethodInstance[] array = methods.toArray(new IMethodInstance[methods.size()]);
        System.out.println("lijst1" + Arrays.asList(array));
        Arrays.sort(array, comparator);
        System.out.println("lijst2" + Arrays.asList(array));

        return Arrays.asList(array);
    }

Krishnan Mahadevan

unread,
Mar 5, 2018, 10:22:09 PM3/5/18
to testng...@googlegroups.com

Dwight,

 

You have only shared how your interceptor looks like.

Can you please also share the following?

 

  1. TestNG version that you are working with. [ Latest released version as of today is 6.14.3]
  2. How do your test class samples look like?
  3. How does your @Priority annotation definition look like.

 

Thanks & Regards

Krishnan Mahadevan

 

"All the desirable things in life are either illegal, expensive, fattening or in love with someone else!"

My Scribblings @ http://wakened-cognition.blogspot.com/

My Technical Scribbings @ http://rationaleemotions.wordpress.com/

Reply-To: <testng...@googlegroups.com>
Date: Tuesday, March 6, 2018 at 3:48 AM
To: testng-users <testng...@googlegroups.com>
Subject: [testng-users] ImethodInterceptor not respected when using testgroup only

--
You received this message because you are subscribed to the Google Groups "testng-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to
testng-users...@googlegroups.com.
To post to this group, send email to
testng...@googlegroups.com.
Visit this group at
https://groups.google.com/group/testng-users.
For more options, visit
https://groups.google.com/d/optout.

DD

unread,
Mar 6, 2018, 3:14:48 AM3/6/18
to testng-users
Hi, 

TestNG version = 6.14.2

TestClass = 
@Priority(1)
public class Login extends BaseScenario
{
      @BeforeMethod(alwaysRun = true)
    public void setup()
    {
        .....
    }

    @Priority(3)
    @Test(description = "....", groups = "Regression")
    public void testLogin()
    {
      ....
    }

@Priority Definition

@Retention(java.lang.annotation.RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD, ElementType.TYPE})
public @interface Priority {
    int value() default 0;
}

Krishnan Mahadevan

unread,
Mar 6, 2018, 10:39:36 PM3/6/18
to testng...@googlegroups.com

Dwight,

 

How about you sharing a full-fledged test sample (with just some sysouts), that I can use to reproduce the problem?

 

I am hoping that the sample would contain the following:

  1. Test classes
  2. Annotation (u have already provided that)
  3. Interceptor (u have already provided that)
  4. Suite xml which you use to execute the tests.

 

 

Thanks & Regards

Krishnan Mahadevan

 

"All the desirable things in life are either illegal, expensive, fattening or in love with someone else!"

My Scribblings @ http://wakened-cognition.blogspot.com/

My Technical Scribbings @ http://rationaleemotions.wordpress.com/

Reply-To: <testng...@googlegroups.com>
Date: Tuesday, March 6, 2018 at 1:44 PM
To: testng-users <testng...@googlegroups.com>
Subject: Re: [testng-users] ImethodInterceptor not respected when using testgroup only

--

You received this message because you are subscribed to the Google Groups "testng-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to


To post to this group, send email to

DD

unread,
Mar 7, 2018, 8:44:54 AM3/7/18
to testng-users
I've send you a PM
Reply all
Reply to author
Forward
0 new messages