Hi,
My @BeforeSuite annotations work perfectly fine when my test classes
count is equals or less than 11 and as soon as I add another test
class, @BeforeSuite annotations starts behaving abnormally. I have two
@BeforeSuite annotations where second one depends on first one such
that second one uses an object (dependency) set by first one. The only
case of getting a NPE is that second method get called before first
method, which should never happen because I've explicitly stated
second method's dependency on first method using 'dependsOnMethods'
annotation attribute. But, I am getting a NPE as soon as my test
classes count exceed 11. I'm experiencing the same behavior in another
project except that the NPE is thrown when my test classes count
exceed 5 instead of 11.
I've attached project bundle (16k). Its a very simple project which
uses maven, testng 5.9 and spring and here's a brief desc of project's
artifacts:
(1). /src/test/java/testing/shared/BaseTestHelper.cass - is an
abstract which override method
'springTestContextPrepareTestInstance()' of
AbstractTestNGSpringContextTests.class as follows:
@Override
@BeforeSuite(alwaysRun = true)
@BeforeClass(alwaysRun = true)
public void springTestContextPrepareTestInstance() {
try
{
System.out.println(" ****** Start Injecting Dependencies ****** ");
System.out.println("this = " + this);
try
{
System.out.println("this.applicationContext = " +
this.applicationContext);
super.springTestContextPrepareTestInstance();
System.out.println("applicationContext injected...");
System.out.println("this.applicationContext = " +
this.applicationContext);
}
catch(Exception e)
{
e.printStackTrace();
}
try
{
System.out.println("this.dependency = " + this.dependency);
this.dependency = this.applicationContext.getBean("dependency");
System.out.println("dependency injected...");
System.out.println("this.dependency = " + this.dependency);
}
catch(Exception e)
{
e.printStackTrace();
}
System.out.println("All dependencies injected SUCCESSFULLY !");
}
finally
{
System.out.println(" ****** Finished Injecting
Dependencies ****** ");
}
}
This method simply inject 'dependency' object using Spring's
ApplicationContext.
This class also have one simple log method against each of the @Before
and @After annotations (Suite, Test, Class, Groups, Method). These log
methods also make a call to 'dependency.toString()' while logging,
just to make sure if there's a missing dependency the method should
throw an NPE and fail.
Also note that the log method which is also annotated with
@BeforeSuite annotation clearly define test method dependency on above
mentioned 'springTestContextPrepareTestInstance()' method as:
@BeforeSuite(dependsOnMethods = "springTestContextPrepareTestInstance")
public void testBeforeSuite (ITestContext testContext)
{
System.out.println("\n ########## SUITE : " +
testContext.getSuite().getName() + " START ########## [this=" + this +
", dependency=" + dependency.toString() + "]\n");
}
(2). /src/test/java/testing/helper/TestHelper.class - an abstract
class derived from BaseTestHelper which contains context conf and a
common method:
@ContextConfiguration(locations = {"/spring.xml"})
@Test(groups = "Group1")
public abstract class TestHelper extends BaseTestHelper {
protected void printClassAndMethod ()
{
StackTraceElement ste = Thread.currentThread().getStackTrace()[2];
System.out.println("Inside " + ste.getMethodName() + "() of "
+ ste.getClassName());
}
}
(3). /src/test/java/testing/helper/MyTestListener.class - a simple
listener which extends TestListenerAdapter of TestNG and logs all
"on*" methods.
(4). /src/test/java/testing/Test1.class TO
/src/test/java/testing/Test11.class - simple test classes with 3
methods each.
-----------------------------------------------
S M Humayun
Software Architect, IBL Group
-----------------------------------------------
Cell : 92.321.8707783
Email (Official) :
smhu...@iblgrp.com
Email (Personal) :
smhu...@gmail.com
Google Talk :
smhu...@gmail.com
MSN :
smhuma...@hotmail.com
Yahoo :
humayun...@yahoo.com
Skype : smhumayun
AIM : syedmohdhumayun
Blog :
http://smhumayun.blogspot.com
LinkedIn :
http://www.linkedin.com/in/smhumayun
Facebook :
http://www.facebook.com/smhumayun
Twitter :
http://twitter.com/smhumayun
Flickr :
http://www.flickr.com/photos/smhumayun
YouTube :
http://www.youtube.com/user/smhumayun
-----------------------------------------------