TestNG output questions

In this article we will cover some TestNG output questions which are usually asked in the interview written tests. The questions are bases upon the program execution flow which will help you grasp some grip over it.

TestNG Output Questions

  1. Guess the output of below program
public class AnnotationsSequence {

@Test
public void f() {
    System.out.println("@Test 1 ");
}
@Test
public void f1() {
    System.out.println("@Test 2 ");
}

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

@AfterMethod
public void afterMethod() {
    System.out.println("@AfterMethod");
}

@BeforeTest
public void beforeTest() {
    System.out.println("@BeforeTest");
}

@AfterTest
public void afterTest() {
    System.out.println("@Aftertest");
}

@BeforeSuite
public void beforeSuite() {
    System.out.println("BeforeSuite");
}

@AfterSuite
public void afterSuite() {
    System.out.println("@AfterSuite");
}

@BeforeClass
public void beforeClass() {
    System.out.println("@BeforeClass");
}

@AfterClass
public void afterClass() {
    System.out.println("@AfterClass");
}

Output of above program(STCM) -
@BeforeSuite
@BeforeTest
@BeforeClass
@BeforeMethod
@Test 1
@AfterMethod
@BeforeMethod
@Test 2
@AfterMethod
@AfterClass
@AfterTest
@AfterSuite

Explanation - Before Test and After Test will run only once, however before method and after method depends upon your @Test methods, if you will declare 100 @Test methods, they will run 100 times.

2. Guess the output of below example -

public class AnnotationsSequence {

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

@AfterMethod
public void afterMethod() {
    System.out.println("@AfterMethod");
}

@BeforeTest
public void beforeTest() {
    System.out.println("@BeforeTest");
}

@AfterTest
public void afterTest() {
    System.out.println("@Aftertest");
}

@BeforeSuite
public void beforeSuite() {
    System.out.println("BeforeSuite");
}

@AfterSuite
public void afterSuite() {
    System.out.println("@AfterSuite");
}

@BeforeClass
public void beforeClass() {
    System.out.println("@BeforeClass");
}

@AfterClass
public void afterClass() {
    System.out.println("@AfterClass");
}

public class Class1 extends AnnotationsSequence {

@Test
public void Method1() {
System.out.println("Inside Method 1");
}

@Test
public void Method2() {
System.out.println("Inside Method 2");
}

public class Class2 extends AnnotationsSequence {

@Test
public void Method3() {
System.out.println("Inside Method 3");
}

@Test
public void Method4() {
System.out.println("Inside Method 4");
}

Output of above program 
@BeforeSuite
@BeforeTest
@BeforeClass
@BeforeMethod
Inside Method 1
@AfterMethod
@BeforeMethod
Inside Method 2
@AfterMethod
@AfterClass
@BeforeClass
@BeforeMethod
Inside Method 3
@AfterMethod
@BeforeMethod
Inside Method 4
@AfterMethod
@AfterClass
@AfterTest
@AfterSuite

The above programs can be easily executed using TestNG.xml file, let us show you how it is achieved. 

<suite name = "Sample suite">
    <test name ="test 1">
        <classes>
            <class name ="PackageName.AnnotationsSequence"> </class>
            <class name ="PackageName.Class1"> </class>
            <class name ="PackageName. Class2"> </class>
        </classes>
    </test>
</suite>

Execute the test suite from TesntNg.xml file itself.

Comments

Popular posts from this blog

Azure Tutorials Series - Azure Networking

Coforge Interview Questions | Automation Testing profile

Testing in CI/CD