Bird
0
0

Consider the following TestNG class:

medium📝 Predict Output Q13 of 15
Selenium Java - TestNG Integration
Consider the following TestNG class:
public class SampleTest {
  @BeforeMethod
  public void setup() {
    System.out.println("Setup");
  }

  @Test
  public void test1() {
    System.out.println("Test1");
  }

  @Test
  public void test2() {
    System.out.println("Test2");
  }

  @AfterMethod
  public void cleanup() {
    System.out.println("Cleanup");
  }
}
What will be the order of printed lines when this test class runs?
ATest1 Test2 Setup Cleanup
BSetup Cleanup Test1 Setup Cleanup Test2
CSetup Test1 Test2 Cleanup
DSetup Test1 Cleanup Setup Test2 Cleanup
Step-by-Step Solution
Solution:
  1. Step 1: Understand TestNG execution flow

    For each @Test method, @BeforeMethod runs before it and @AfterMethod runs after it.
  2. Step 2: Trace the print statements

    First test: Setup -> Test1 -> Cleanup. Second test: Setup -> Test2 -> Cleanup.
  3. Final Answer:

    Setup Test1 Cleanup Setup Test2 Cleanup -> Option D
  4. Quick Check:

    BeforeMethod and AfterMethod wrap each test [OK]
Quick Trick: BeforeMethod and AfterMethod run around each test [OK]
Common Mistakes:
  • Assuming BeforeMethod runs once before all tests
  • Assuming AfterMethod runs once after all tests
  • Mixing order of print statements

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Selenium Java Quizzes