@BeforeClass and inheritance - order of execution

前端 未结 16 2038
梦如初夏
梦如初夏 2020-12-02 11:39

I have an abstract base class, which I use as a base for my unit tests (TestNG 5.10). In this class, I initialize the whole environment for my tests, setting up database map

相关标签:
16条回答
  • 2020-12-02 12:31

    I've just gone through this and found one more way to achieve this. Just use alwaysRun on @BeforeClass or @BeforeMethod in the abstract class, works as you would expect.

    public class AbstractTestClass {
        @BeforeClass(alwaysRun = true)
        public void generalBeforeClass() {
            // do stuff
            specificBeforeClass();
        }
    }
    
    0 讨论(0)
  • 2020-12-02 12:32

    For JUnit: As @fortega has mentioned: According to the JUnit api: "The @BeforeClass methods of superclasses will be run before those the current class."

    But be careful not to name both methods with the same name. Since in this case the parent method will be hidden by child parent. Source.

    0 讨论(0)
  • 2020-12-02 12:35

    dependsOnMethod can be used.

    e.g. in case of Spring (AbstractTestNGSpringContextTests)

    @BeforeClass(alwaysRun = true, dependsOnMethods = "springTestContextPrepareTestInstance")
    
    0 讨论(0)
  • 2020-12-02 12:36

    In my case (JUnit) I have the same methods called setup() in the base class and the derived class. In this case only the derived class's method is called, and I have it call the base class method.

    0 讨论(0)
提交回复
热议问题