Friday, July 1, 2011
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 { @Factory public Object[] createTest(){ Object[] res = new Object[3]; for(int i=1;i<=3;i++){ res[i-1]=new main.java.FactoryImplementation(i); } return res; } } public class FactoryImplementation { int c_instance; public FactoryImplementation(int instance){ this.c_instance=instance; } @Test 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:
@Factory public Object[] createInstance(){ ArrayList ar = new ArrayList< Object>(); Object[] res =null; for(int i=1;i<=5;i++){ ar.add(new main.java.FactoryImplementation(i+10)); } res= new Object[ar.size()]; res=ar.toArray(); return res; }
Subscribe to:
Post Comments
(
Atom
)
4 comments :
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
@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.
Can we call another factory method from a factory method?
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