java - @Test is calling Parent class constructor in TestNG Selenium -


i newbie java , please have @ below code

//parent class  public class abc {     abc(){system.out.println("hiii");     } }  //child class  public class cde extends abc{     @test     public void xyz(){         system.out.println("hi");     }     }     output coming  hiii hi passed: xyz 

please , not sure why constructor of parent class getting called when not using new keyword create object.i have created 2 different classes in eclipse.same not happening if creating main method in child class ie not using testng @test annotation.

when use @test annotation testng, here's testng does, behind scenes.

  • it first loads class.
  • it makes list of testng annotated methods run (for e.g., @test, @beforeclass etc.,) , orders them in testng way (i.e., @beforesuite, @beforetest, @beforeclass, @test , on)
  • it finds default constructor in class. if no default constructor found, tries 1 argument constructor takes string.
  • using reflection class instantiated.
  • now using instance of test class obtained via reflection, testng starts calling various annotated methods in pre-defined order.

the reason why base class constructor being invoked, due testng instantiating child class via reflection, triggers constructor chain inheritance ladder (which called constructor chaining)

you can test theory adding constructor child class cde , adding system.out.println. see print statement being executed.

when via main() method, there's no reflection involved, because testng not involved.


Comments

Popular posts from this blog

What is happening when Matlab is starting a "parallel pool"? -

angular - DownloadURL return null in below code -

php - Cannot override Laravel Spark authentication with own implementation -