IAnnotation transform method is not disabling the test in testng

Here my intention is , Based on the before method condition the @Test method needs to enabled or disabled.But in the below code even though I am passing the test case name its not getting skipped ? Can anyone suggest me solution?

I need the BeforeTestMethod to check some logic in my actual code and based on that I have to enable the @Test in the class file

public class ListnerClass implements IAnnotationTransformer {
public static String testName;
public void transform(ITestAnnotation iTest, Class testClass, Constructor testConstructor, Method method) {
if(method.getName().equalsIgnoreCase(testName)) {
iTest.setEnabled(false);
}

}

public class TestNGTest3 {
@BeforeMethod
public void setUp(Method result) {
System.out.println("This is the before Method getting name "+result.getName());
if(result.getName().contains(“3”))
{
ListnerClass.testName=result.getName();

}

}

@Test
public void testMethod3() {
System.out.println(“This is the Method of Method”);
}

@Test
public void testMethod4() {

System.out.println("Hi");

}

}
TestNG.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name=" Regression Suite">
<listeners>
<listener class-name="com.listners.ListnerClass" />
</listeners>
<test thread-count="1" name="Test">
<classes>
    <class name="com.test.TestNGTest3" />
</classes>



Output:

This is the before Method getting name testMethod3
This is the Method of Method
This is the before Method getting name testMethod4
Hi

With a little google searching, I was able to determine you are talking about TestNG with selenium, it would have been expeditious if you had mentioned that at the beginning.

Perusing other people’s questions on the interwebs and the docs, I suggest that you verify who gets executed first: the listener or the beforeMethod. Put a println in the listener. My guess would be the listener is called before the @BeforeMethod.

Based on the docs I think you may want to use an IInvokeMethodListener instead.

1 Like

Iivokemethod listener will do before method but how to restrict the @Test method being executed
@fire-eggs ???

This looks potentially useful.