Search code examples
javatestng

Some of the methods of TestNG Parent class are not running


I have a Parent class BaseClass with TestNG annotations as below

public class BaseClass 
{
     protected static WebDriver driver; 

     @BeforeSuite
     public void beforeSuite()
     { 
       System.out.println("\nBEFORE SUITE : "); 
     }

     @BeforeTest
     public void launch() throws IOException
     {
       //CODE TO LAUNCH BROWSER
     }

     @BeforeClass
     void beforeclass()
     {
       System.out.println("BEFORE CLASS");  
     }

     @AfterClass
     void afterClass()
     {      
       System.out.println("After test ");
     }

     @BeforeMethod
     void beforeMethod()
     {
       System.out.println("BEFORE METHOD ");    
     }

     @AfterMethod 
     void afterMethod ()
     {  
       System.out.println("After METHOD ");
     }

     @AfterSuite
     void afterSuite()
     {
       System.out.println("\n After Suite");
     }
}

and the child class extending it as below

public class SearchQualityRuleTest extends BaseClass
{
    @Test
    public void searchTest() throws InterruptedException  
    {
      //Assertions 
     }  
}

My XML file is as below

<suite name="AAD-AED suite" verbose="1" >
    <test name="AED tests">
      <classes>
             <class name="tests.AED_searchQualityRuleTest"/>          
      </classes>
    </test>
</suite> 

Other than @BeforeSuite and @BeforeTest methods no other methods are running.

I have code related to Extent Reports in other non executed methods.

Please suggest if there is any mistake in the code


Solution

  • There is more than meets the eye.
    Output of your example on my computer:

    BEFORE SUITE : 
    BEFORE CLASS
    BEFORE METHOD 
    After METHOD 
    After test 
    
     After Suite
    

    I bet you have got more classes, which inherit from BaseClass, not just one. Those methods are not executed properly due to reasons similar as behind this. Implementing an InvokedMethodListener would help you to overcome issues with inheritance.