0
0
Selenium Javatesting~20 mins

TestNG annotations (@Test, @BeforeMethod, @AfterMethod) in Selenium Java - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
TestNG Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of TestNG method execution order
Consider the following TestNG test class. What will be the order of printed output when the tests run?
Selenium Java
import org.testng.annotations.*;

public class SampleTest {

  @BeforeMethod
  public void setup() {
    System.out.println("Setup");
  }

  @Test
  public void testA() {
    System.out.println("Test A");
  }

  @Test
  public void testB() {
    System.out.println("Test B");
  }

  @AfterMethod
  public void teardown() {
    System.out.println("Teardown");
  }
}
A2,1,3,5,4,6
B1,3,2,4,6,5
C1,2,3,4,5,6
D2,3,1,5,6,4
Attempts:
2 left
💡 Hint
Remember that @BeforeMethod and @AfterMethod run before and after each @Test method.
assertion
intermediate
1:30remaining
Correct assertion to verify setup execution
You want to verify that the setup method annotated with @BeforeMethod runs before each test. Which assertion correctly checks that the setup flag is set before the test runs?
Selenium Java
import org.testng.Assert;
import org.testng.annotations.*;

public class SetupTest {
  private boolean setupDone = false;

  @BeforeMethod
  public void setup() {
    setupDone = true;
  }

  @Test
  public void testCheckSetup() {
    // Which assertion below is correct here?
  }
}
AAssert.assertEquals(setupDone, false);
BAssert.assertFalse(setupDone);
CAssert.assertNull(setupDone);
DAssert.assertTrue(setupDone);
Attempts:
2 left
💡 Hint
The setupDone flag is set to true in @BeforeMethod before the test runs.
🔧 Debug
advanced
2:00remaining
Identify why @AfterMethod is not running
Given the following TestNG test class, why is the teardown method annotated with @AfterMethod never executed?
Selenium Java
import org.testng.annotations.*;

public class DebugTest {

  @BeforeMethod
  public void setup() {
    System.out.println("Setup");
  }

  @Test
  public void testSomething() {
    System.out.println("Test");
    throw new RuntimeException("Fail");
  }

  @AfterMethod
  public void teardown() {
    System.out.println("Teardown");
  }
}
AThe @AfterMethod method always runs even if the test fails, so the problem is elsewhere.
BThe @AfterMethod method must be static to run after tests.
CThe @AfterMethod method is not running because the test method is failing.
DBecause the test throws an exception, @AfterMethod is skipped.
Attempts:
2 left
💡 Hint
Check TestNG behavior when tests throw exceptions.
🧠 Conceptual
advanced
1:00remaining
Purpose of @BeforeMethod and @AfterMethod in TestNG
Which statement best describes the purpose of @BeforeMethod and @AfterMethod annotations in TestNG?
A@BeforeMethod runs before each test method; @AfterMethod runs after each test method.
B@BeforeMethod runs once before all tests; @AfterMethod runs once after all tests.
C@BeforeMethod and @AfterMethod run only if the test passes.
D@BeforeMethod and @AfterMethod are used to group tests into suites.
Attempts:
2 left
💡 Hint
Think about setup and cleanup for each test method.
framework
expert
2:00remaining
TestNG behavior with multiple @BeforeMethod methods
If a TestNG test class has two methods annotated with @BeforeMethod, how does TestNG execute them before each test method?
ATestNG runs both @BeforeMethod methods in the order they appear in the class before each test.
BTestNG runs both @BeforeMethod methods but the order is not guaranteed before each test.
CTestNG throws a runtime error if multiple @BeforeMethod methods exist.
DTestNG runs only one @BeforeMethod method chosen at random before each test.
Attempts:
2 left
💡 Hint
Consider TestNG's handling of multiple configuration methods of the same type.