Friday, July 1, 2011

Factory Class in TestNg

There are many functions provided by TestNG and you can use them in different ways I will mention in this blog about the @Factory implementation.

@ Factory
A Factory will allow to create test dynamically. And whenever we define a Factory it should always return an object array “Object[]”. All the test methods under the Class object which is created and returned using the Factory method is automatically executed by the Factory method. For ex.
public class FactoryClass {

public Object[] createTest(){
   Object[] res = new Object[3];
   for(int i=1;i<=3;i++){
   return res;

public class FactoryImplementation {
int c_instance;
public FactoryImplementation(int instance){
public void printMethod(){
   System.out.println("Instance Num is "+ c_instance);

Whenever you execute the above two classes together as a TestNg test, the Output will be as follows:
Instance Num is 1
Instance Num is 2
Instance Num is 3

You only have to execute the Class named “FactoryClass”. Test-NG will find the Factory method inside it and will execute it. For each loop inside the Factory method an object of FactoryImplementation Class is created. On creation of the FactoryImplementation object and Instance value is set using the constructor. While creation of FactoryImplementation object the Test-NG automatically searches for all the “Test” methods inside the FactoryImplementation class and add them to its test execution list.
Another thing for you to notice is that each object created have a unique “c_instance” value set while object creation and the same value is used in the “printMethod”.

The Factory method can be used in different ways. One of the case may be when you want to execute a certain number of test inside a class for different values that can increase or decrease based on a condition.
This can be achieved by creation of the object of test class based on the number of values.
In the above class I am creating the object of the FactoryImplementation class inside a "for" loop. In a similar way you can iterate the for loop for number of different values for which the test needs to be run. And then initializing the test class object with such values and returning the Object array after storing them in the array. Something like the following:

public Object[] createInstance(){
ArrayList ar = new ArrayList< Object>();
Object[] res =null;
for(int i=1;i<=5;i++){
res= new Object[ar.size()];
return res;

In the above method I am initializing the ArrayList which is dynamic in nature so depending on the no. of iterations I can increase the Array size and then store the same to the Object array and return it for execution.


Anik said...

Hello Varun... could you please give me the code for checking email from gmail account every minute and pop up messgage if finds one with a particular subject?????? ..offcourse using javaMail api

Varun Menon said...

@Anik - Hi Anik I cant help you with the said code as I havent worked on the said scenario. Suggest you to use the google search. Hopefully you will find what you are looking for.

Archana said...

Can we call another factory method from a factory method?

Varun Menon said...

If the second factory some @Test in it then yes. Else if you are expecting first factory to create subfactories for creation of test classes then No.

Post a Comment