Modification in custom annotation at run time

710 views
Skip to first unread message

Abhishek Bisht

unread,
Aug 18, 2022, 3:33:00 AM8/18/22
to testng-users
Hi team,

I have multiple customAnnotaion defined at method level 
Ex-
@Test()
@customannotation1(author = "abc")
@customannotation2(team = "abc")
public void test(){
//testing some code here

}

Now I want to modify the declared custom annotation , while using IAnnotationTransformer 
public class TestExample implements IAnnotationTransformer {
@Override
public void transform(ITestAnnotation annotation, Class testClass,
Constructor testConstructor, Method testMethod) { 
}
I can get all declared annotaion from testMethod but how can I edit and save again those annotations

Regards
Abhishek

⇜Krishnan Mahadevan⇝

unread,
Aug 18, 2022, 3:43:33 AM8/18/22
to testng...@googlegroups.com
Short answer: You can't use a simple API to do this.

Annotations were never meant to be changed at runtime to begin with. 

Then you might ask, "Well, TestNG lets me alter TestNG annotation value via an IAnnotationTransformer no?".

Yes. That's true. But TestNG does not do this by manipulating annotation values (which is essentially done via Reflection).

For doing this TestNG uses a design wherein for every annotation found, there's a corresponding interface that mimics the annotation's capabilities, and then there's an actual implementation of this capability interface that represents the values that were set when the annotations were parsed.

The annotation transformer is basically just exposing you to the capability interface and when you toggle/set values via this interface, TestNG alters the state of the corresponding object, and later TestNG just reads the values of the state object to decide what to do. This kind of gives you a feel as if annotation values are being toggled at runtime.

Now coming back to how to actually do it, I would suggest that you take a look at this article : 


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/c9e4bf3c-b6bc-4915-9151-2b72f6c8130fn%40googlegroups.com.

Boris Perelshteyn

unread,
Aug 19, 2022, 1:53:00 AM8/19/22
to testng-users
Abhishek,

Do you think using Groups would be useful in what you are attempting to achieve ?

Sincerely,
Boris.

Abhishek Bisht

unread,
Aug 19, 2022, 2:47:59 AM8/19/22
to testng-users
No Boris actually there are multiple data passing from these customs annotations on which there are multiple test decisions are taken.
@Krishna - Have tried the above block the problem is above one is to add annotation on class level while here we need to add an annotation at method level.
Even I have added annotation but not able to inject those added annotation to testmethod -
______________________
 Class<?> superclass = testMethod.getClass().getSuperclass();
// Method method =
Field declaredField = null;
try {
declaredField = superclass.getDeclaredField("declaredAnnotations");
declaredField.setAccessible(true);
Map<Class<? extends Annotation>, Annotation> map =(Map<Class<? extends Annotation>, Annotation>) declaredField
.get(testMethod);
map.put(NewAnnotation.class,NewAnnotationObject) ;
declaredField.set(testMethod,map);

_____________________________

So at method scope level testMethod has now new annotation attached but on suite level that value is not yet set.

Any help will be highly appreciated.

Regards
Abhishek

⇜Krishnan Mahadevan⇝

unread,
Aug 19, 2022, 3:36:29 AM8/19/22
to testng...@googlegroups.com
I didn't quite understand what you were saying. Can you please explain once again?

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/

Abhishek Bisht

unread,
Aug 19, 2022, 3:55:43 AM8/19/22
to testng-users
got the Answer to this above solution is working fine only need to declare at the right place, for example, I was using the above code at the listener implementing IAnnotationTransformer2 and new annotation was added successfully but was trying to retrieve the same at IInvokedMethodListener listener on IInvokedMethod method level as a result was not getting value.
On  declaring same code on beforeinvocation at IInvokedMethodListener I am able to set a new annotation at Isuite level

Thanks everyone for help
Reply all
Reply to author
Forward
0 new messages