0

How this @Factory annotation works, when we need to use @Factory

public class Factory1 
    {

        @Factory
        public Object[] testMy()
        {
            return new Object[]{new Login1()};

        }


    }

Please tell me what below code does,

return new Object[]{new Login1()}
J_Coder
  • 707
  • 5
  • 14
  • 32

1 Answers1

1

Sometimes we may need to run a set of tests with different data values. To achieve this we may define a separate set of tests inside a suite in the testng XML and test the required scenario. The problem with this approach is that, if you get an extra set of data, you will need to redefine the test.@Factory allows tests to be created at runtime depending on certain data-sets or conditions.

Let's take your example

   @Factory
            public Object[] testMy()
            {
                return new Object[]{new Login1()};

            }

    public class login{

    public login(){

    syso('Login constructor called');

    }

output :

Login constructor called

You can also pass arguments and call the constructor multiple times

@Factory
            public Object[] testMy()
            {
                return new Object[]{new Login1(1),new Login1(2)};

            }

  public class login{

    public login(int num){

    syso('The number is '+num);

    }

output:

The number is 1

The number is 2

Hope this helps you..Kindly get back if you have any queries

Vicky
  • 2,999
  • 2
  • 21
  • 36
  • Thanks @Vicky for eg: return new Object[]{new TestA(2),new TestA(3),new TestA(1)}; But my output comes in the order of TestA(3),TestA(1) and TestA(2). Please tell me how this sequence works? – J_Coder Jul 05 '15 at 09:52
  • @Seetharaman can u pls post your code so that i can explain it more clearly to you by taking it as an example – Vicky Jul 05 '15 at 12:40