2

I have a setUp() in my SuperTest class. Now in my ChildTest class, I want to set up another setUp(). This one will run specifically for ChildTest only.

So I currently have this

    //SuperClass
    protected void setUp(ITestContext context) {
        ...
    }

    //SubClass extends SuperClass
    @BeforeMethod
    protected void setUp(ITestContext context) {
        super.setUp(context);
        ...
    }

    //ChildClass extends SubClass
    @Override
    @BeforeMethod
    protected void setUp(ITestContext context) {
        super.setUp(context);
        ...
    }

The problem is, when I run ChildTest, it runs both of the setUp() from SubClass and it's own... How can I get it so it'll only run it's own setUp()?

2 Answers2

0

No it should not run both the setup methods, if you have extended the parent class and followed all the rules of overriding a method then there in no way your parent class method is going to get executed. Consider the following scenario. I have two TestNG classes with Abc as the parent class and Def as the child class

public class Abc{

@BeforeMethod
public void beforeMethod(ITestContext context) {
    System.out.println("PARENT");
}

}

public class Def extends Abc{

@BeforeMethod
public void beforeMethod(ITestContext context) {
    System.out.println("CHILD");
}

@Test
public void test(){
    System.out.println("TEST");
}

}

Now if I execute the Def class as TestNg Test, it is definitely going to execute beforeMethod of the child class and then after that the test method of the child class

Rishi Khanna
  • 409
  • 1
  • 5
  • 16
  • But it does run both. I had to comment out the super class setUp for the child class to run it's own setUp. I also did what you did and print out on console, it clearly shows "super" and "child" –  Sep 22 '14 at 16:58
  • I am not able to figure out what are you missing upon but when I execute the above scenario it prints: CHILD and then TEST – Rishi Khanna Sep 22 '14 at 17:24
0

Reference to this answer: https://stackoverflow.com/a/3456599

I ended up adding a method in the SubClass that calls the setUp of SuperClass

    //SubClass extends SuperClass
    protected void getSuperSuper((ITestContext context) {
        super.setUp(context);
    }

    //ChildClass extends SubClass
    protected void setUp(ITestContext context) {
        super.getSuperSuper(context);
        ...
    }
Community
  • 1
  • 1