简体   繁体   中英

Is it possible to give @AfterXXX TestNG annotations in a Java class which doesn't follow inheritance chain?

I have 3 Java classes in the sampe project but in different packages: Class A, B and C.

Class B extends Class A. Class B is my main class in which I have given the @Test annotation over several methods as shown below:

Class B extends A {

  @Test
  public void Method1(){
      ...
  }

  @Test
  public void Method2(){
      ...
  }

}

Now in the Class A, which is the parent of Class B contains various @BeforeXXX and @AfterXXX annotated methods and also other methods such as shown below:

Class A {

  @BeforeTest
  public void Method3(){
      ...
  }

  @BeforeMethod
  public void Method2(){
      ...
  }

  @AfterMethod
  public void Method2(){
      ...
  }

}

Now, I have a class C which has no relation with the main class B. But I want to put a method in that class C with the @AfterSuite annotation. During debugging, I found that the execution was not coming to the @AfterSuite annotated method in the Class C.

So, can anyone suggest something through which I can achieve what I want?

I think the solution to your problem is really easy. All you need to do is put the @AfterSuite annotation to whether Class A, or Class B.

It is a good practice to use @BeforeSuite + @AfterSuite only in one place (so you know what loads before and after the suite instead of having to go to all classes and concat the result. Also if you want to change something in the future it will be easier.

But the answer to your question is put it in Class A:

Class A {

  @BeforeTest
  public void Method3(){
      ...
  }

  @BeforeMethod
  public void Method2(){
      ...
  }

  @AfterMethod
  public void Method2(){
      ...
  }

  @AfterSuite
  public void afterSuite(){
      ...
  }

}

Please feel free to post a comment below if you have any issues; but yes the @AfterSuite annotation will be executed after all the tests of the suite have finished. So hypothetically speaking that this was your XML file to start TestNG:

<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="AfterSuite">


  <test name="test1">
    <classes> 
     <class name="tests.A"/>        
     </classes>
  </test>

    <test name="test2">
    <classes> 
     <class name="tests.B"/>        
     </classes>
  </test>

   <test name="test3">
    <classes> 
     <class name="tests.C"/>        
     </classes>
  </test>


</suite>

It does not really matter where you will have the @AfterSuite annotation as long as all the classes in the suite are actual tests. You mentioned something that your debug showed that class C is not reached; so if you created C just for the @AfterSuite annotation you probably won't need it at all. In fact the @AfterSuite annotation will work even if you place it in class B.But to my understanding your TestBase is class A (as long as all the rest of the classes extend it) so it will be probably the better place to put it.

Best of luck! :)

PS. If you want another solution WITHOUT inheritance (ie to still keep class C) all you need to do is to create an obj of class A into class C. So for example:

Class C {
A objectOfClassA = new A();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM