cbeust/testng — Issue #130: AfterClass method executed many times
Author: asifabdul
Created: 2011-11-17 23:48:35+00:00
State: open

I have defined few tests in a Base class (BaseTest) and the remaining in a Sub class(LoginTest). Now when I execute LoginTest, I see that tearDown is invoked two times and it is invoked before the execution of testStringLength method. The sample output is as follows,

===============================================================

in com.aquafold.learning.test.inheritance.problem.LoginTest@535b58 BaseTest.setup
in testParameters
in com.aquafold.learning.test.inheritance.problem.LoginTest@535b58 BaseTest.tearDown
in testStringLength
in com.aquafold.learning.test.inheritance.problem.LoginTest@535b58 BaseTest.tearDown

===============================================================

I am using IntelliJ Idea plugin to run testng.

The result is same when I run from Windows command line using tesng Implementation-Version: 6.3-201110161418

public abstract class BaseTest {

@BeforeClass
public void setup() {
System.out.println("in " + this + " BaseTest.setup");
// connect to db
}

@AfterClass
public void tearDown() {
System.out.println("in " + this + " BaseTest.tearDown");
// release connection to db
}

public abstract Map getParameters();

@Test(groups = {"abc"})
public void testParameters() {
System.out.println("in testParameters");
Map map = getParameters();
Assert.assertNotNull(map, "Parameters cannot be null");
}

}

And my subclass defines few more test cases.

public class LoginTest extends BaseTest {

@Override
public Map getParameters() {
Map params = new HashMap();
params.put("username", "mike");
params.put("password", "123");
return params;
}

@Test
public void testStringLength() {
System.out.println("in testStringLength");
Set> entries = getParameters().entrySet();
for (Map.Entry entry : entries) {
String value = (String) entry.getValue();
Assert.assertTrue(value.length() <= 10, "Maximum allowed length is 10");
}
}
}