Bird
0
0

Consider a TestNG class with multiple @BeforeMethod and @AfterMethod methods. How can you control the order in which these methods run?

hard📝 Application Q9 of 15
Selenium Java - TestNG Integration
Consider a TestNG class with multiple @BeforeMethod and @AfterMethod methods. How can you control the order in which these methods run?
AUse the 'priority' attribute in @BeforeMethod and @AfterMethod
BTestNG does not support ordering for @BeforeMethod and @AfterMethod
CUse the 'alwaysRun' attribute in @BeforeMethod and @AfterMethod
DUse the 'dependsOnMethods' attribute in @BeforeMethod and @AfterMethod
Step-by-Step Solution
Solution:
  1. Step 1: Understand ordering in TestNG configuration methods

    TestNG does not guarantee any specific order for multiple configuration methods with the same annotation.
  2. Step 2: Evaluate options

    Priority and dependsOnMethods are only for @Test methods, not configuration methods. alwaysRun controls execution conditions but not order. Thus, B is correct.
  3. Final Answer:

    TestNG does not support ordering for @BeforeMethod and @AfterMethod -> Option B
  4. Quick Check:

    No guaranteed order for config methods = B [OK]
Quick Trick: Use dependsOnMethods to order @BeforeMethod/@AfterMethod [OK]
Common Mistakes:
  • Using priority attribute on configuration methods
  • Assuming alwaysRun controls order
  • Believing TestNG cannot order these methods

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Selenium Java Quizzes