Reputation: 2422
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)
}
}
Upvotes: 7
Views: 10959
Reputation: 426
TestNG supports native injection of some parameters, for example the information about the method to be called. So you could use the following approach:
import java.lang.reflect.Method;
...
@BeforeMethod
public saveRecord(Method method){
if (!method.getName().equals("saveTest")) {
session=getCurrentSessionFactory()
myEntity = new myEntity();
myEntityId = session.save(myEntity)
}
}
http://testng.org/doc/documentation-main.html#native-dependency-injection
Upvotes: 3
Reputation: 172
you can groups in testing it will call a separate beforeMethod for different group http://testng.org/doc/documentation-main.html#test-groups
Upvotes: 1