Custom annotation invoking speciifc method

32 views
Skip to first unread message

Ashish S

unread,
Dec 21, 2021, 1:29:01 AM12/21/21
to testng-users

I have a requirement as below. Please suggest how we can implement it using TestNG and annotations.
1. Create a Custom annotation and value will be passed in it. Then, it will invoke one method which will perform some work and will have Asserts in it as well.
2. @Test will be used as TestRunner mainly and Pass/Fail of this TestMethod will appear in reports based on outcome of above method.
Note: @Test method will not contain any code.

@Test
@CustomAnnotation(abc.json)
public void test() {
// No Code Here
}

Thanks,
Ashish

⇜Krishnan Mahadevan⇝

unread,
Dec 21, 2021, 1:33:36 AM12/21/21
to testng-users
Couple of questions.
  1. What is the annotation going to represent? 
  2. Who would be parsing this annotation ? You can use custom annotations to do extra work (such as automatically spawn a browser or launch a docker container etc), but you can't have a custom annotation double up as a test code representative. If yes, then you would need to share additional context into what exactly are you trying to do because it sounds like you are trying to create a parallel test runner like TestNG, but need TestNG to run the tests which kind of sounds confusing to me.

Have you tried looking at IHookable ? It sounds like that can help you do what you are asking for (including ability to parse your custom annotation as well)

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 Scribblings @ https://rationaleemotions.com/


--
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 view this discussion on the web visit https://groups.google.com/d/msgid/testng-users/94af97f1-143b-4c8b-8203-db707bf9ef35n%40googlegroups.com.

Ashish S

unread,
Dec 21, 2021, 2:40:19 AM12/21/21
to testng-users
I am thinking of invoking another method based on values passed in annotation and it will help in partial or full completion of test code. It's sort of method call without explict calling in test method, with parameter values from annotation.

I think we can handle this in beforeInvocation of IInvokedMethodListener as well. I will explore iHookable listener as well.

Thanks,
Ashish

⇜Krishnan Mahadevan⇝

unread,
Dec 21, 2021, 2:43:53 AM12/21/21
to testng-users
If you would like your annotation driven method references to be part of the test, then I dont think you can do that via the listeners, because you may not be able to affect the test result from within a listener.

The IHookable way would be more apt, because you would be getting a handle to the ITestResult object of the test and you can perhaps alter its state easily there.

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 Scribblings @ https://rationaleemotions.com/

Ashish S

unread,
Dec 21, 2021, 3:33:27 AM12/21/21
to testng-users
Thanks for your inputs.
I am not clear with your first statement on affect the test result from within a listener  . I tried with below simple code to check if any failure in listener is marking overall test method as pass/fail and it's marking - whether we use Assert or setStatus using ITestResult object based on some actions outcome.
Please share if you want to address any specific case constraint here.

// Annotation
import java.lang.annotation.*;

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface CustomAnnotation {
        String testCaseId() default "";
}

// Listener
import org.testng.Assert;
import org.testng.IInvokedMethod;
import org.testng.IInvokedMethodListener;
import org.testng.ITestResult;

public class Listener implements IInvokedMethodListener {

        @Override
        public void beforeInvocation(IInvokedMethod method, ITestResult testResult) {
                // Check for Annotation
                if (!method.getTestMethod().getConstructorOrMethod().getMethod()
                                .getAnnotation(core.parser.CustomAnnotation.class).testCaseId().isEmpty()) {
                        customMethod(method.getTestMethod().getConstructorOrMethod().getMethod()
                                        .getAnnotation(core.parser.CustomAnnotation.class).testCaseId());
                }
        }

        @Override
        public void afterInvocation(IInvokedMethod method, ITestResult testResult) {

        }

        private static void customMethod(String testCaseID) {
                if (testCaseID.equalsIgnoreCase("fail")) {
                        Assert.fail();
                }
        }
}


// Test Cases
        @Test
        @CustomAnnotation(testCaseId="PASS")
        public void testsuccess() {

        }
       
        @Test
        @CustomAnnotation(testCaseId="FAIL")
        public void testfailure() {

        }

⇜Krishnan Mahadevan⇝

unread,
Dec 21, 2021, 4:21:59 AM12/21/21
to testng-users
Ashish,

What I was suggesting was that, I am not sure if from within a listener you would be able to control the outcome of a test method's result. In the past I remember fixing some bugs around this. If it works fine, then we are good. Feel free to pick an approach that best works for you.


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 Scribblings @ https://rationaleemotions.com/

Reply all
Reply to author
Forward
0 new messages