We have a separate Java project completely dedicated to functional testing of our web application with TestNG and Selenium (we refer to it as the Test Suite). It is pretty sophisticated and uses Spring for dependency injection extensively.
The entry point, where Application context is initialized and the beans used to initialize other beans are defined, is the AbstractTest
class, which is a base class for all test classes in the suite, and is declared like this:
@ContextConfiguration("classpath*:applicationContext*.xml")
public class AbstractTest extends AbstractTestNGSpringContextTests {
...
Now I need to add two listeners:
public class CustomSuiteListener implements ISuiteListener
— to perform some actions before our tests run (e.g. log in to the web application and set it up via Web UI), andpublic class CustomTestMethodInterceptor implements IMethodInterceptor
— to filter away those methods that should not run this time based on external decision mechanism (e.g. known open bugs).
Both these listeners require to load the application context because all settings (web application URL, credentials, database etc) are declared there; also bean autowiring would be of much help there.
For now, in both listeners I have to explicitly create a new application context in this fashion:
public class CustomSuiteListener implements ISuiteListener {
@Override
public void onStart(ISuite suite) {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("classpath*:applicationContext*.xml");
WebDriverProvider provider = (WebDriverProvider) applicationContext.getBean("webDriverProvider");
ObjectInitializer objectInitializer = (ObjectInitializer) applicationContext.getBean("objectInitializer");
// other beans...
The problem is that, in addition to application context init in AbstractTest
, every such call results in application context being loaded again and again; given that the suite is already big and is promising to grow fast, this results in a huge time waste.
The question is: is there any way to initialize application context once and then use it universally within our Test Suite?