TestNG: How to skip @BeforeMethod call for some tests

Viewed 8985

I am using TestNG for unit testing. I am using @BeforeMethod to save record and then execute the update, search, delete tests.

I am looking for the option where I can avoid executing @BeforeMethod method call for some test cases. For example, I have three tests save, update and delete. In this case, I only want to call @BeforeMethod for update and delete test not for save test. Any Idea?

Please note that, I don't want to use @DependesOnMethods as it is not recommended.

Thanks in advance.

My Test class looks like below:

@ContextConfiguration("file:application-context-test.xml")
@TransactionConfiguration(transactionManager = "transactionManager", defaultRollback = true)
public class MyEntityTest {

    int long myEntityId; 

    @BeforeMethod 
    public saveRecord(){
      session=getCurrentSessionFactory()
      myEntity = new myEntity();
      myEntityId = session.save(myEntity)
    }

    @Test
    public saveTest(){
      session=getCurrentSession()
      myEntity =session.getByID(myEntityId)
      session.save(myEntity)
    }

    @Test
    public updateTest(){
      session=getCurrentSession()
      myEntity =session.getByID(myEntityId)
      session.update(myEntity)
    }
}
2 Answers
Related