Mona
Mona

Reputation: 352

I am trying to use @DataProvider to run @test with Different Parameters, but getting exception

i want to pass parameter to @test to run multiple times with different data. so i am using @DataProvider which is retuning two dimensional Object array. i am having one dimensional string so at first i am converting it to two dimensional array followed by assigning value to objects. . but getting following exceptions.

public class DtaProvider {

    public static String patchfileName = null;
    public static String[] patchsplit = null;
    public static String temp= null;
    public int number   = 0;

      @DataProvider(name = "getData")
      public Object[][] createData() {

          patchfileName = "hi,how,are,you";
          patchsplit = patchfileName.split(",");

          Object[] arr = patchsplit ;

          System.out.println(arr.length);

          for(int i=0;i<arr.length;i++){
              System.out.println(arr[i].toString());
          }
          Object[][] data = new Object[arr.length][arr.length];

          for (int x = 0; x < arr.length; x++){
              data[x][x] = arr[x];

            }

          return data;

      } 



        @Test(dataProvider="getData")

        public void DownloadPatch(String patchfileNamea ){

            try{

            System.out.println("Name is b"+patchfileNamea);
            }
            catch (Exception e){
                org.testng.Assert.fail("Failed to Download Patch to NgDriver " + e.getMessage());

            } 
        }
}


TestNG] Running:
  C:\Users\Mohan Raj S\AppData\Local\Temp\testng-eclipse-1472908796\testng-customsuite.xml

4
hi
how
are
you
FAILED: DownloadPatch
org.testng.TestNGException: 
The data provider is trying to pass 4 parameters but the method testclasses.DtaProvider#DownloadPatch takes 1
    at org.testng.internal.Invoker.injectParameters(Invoker.java:1257)

kindly help me how to pass parameter sequentially to my @test Method?

Upvotes: 0

Views: 1901

Answers (2)

shashin
shashin

Reputation: 51

If you know the parameters that you want to pass, then the following can be used to pass a single parameter to your @Test method.

@DataProvider(name = "getData")
public Object[][] createData() {
    return new Object[][] { { "hi" }, { "how" }, { "are" }, { "you" } };
}

Upvotes: 1

Priyanshu
Priyanshu

Reputation: 3058

It is clearly mentioned in error log that @Test method is taking only one parameter while @DataProvider is returning four parameters.

It is pretty simple that the number of parameters @DataProvider returns same should be passed in @Test method. Your @Test method should look like:

@Test(dataProvider="getData")
public void DownloadPatch(String param1, String param2, String param3, String param4){
   System.out.println(param1);
}

Update:1

Even if you have some reason to pass only one param in your @Test method then you need to modify your @DataProvider in this way.

public Object[][] createData() {
    patchfileName = "hi,how,are,you";
    patchsplit = patchfileName.split(",");
    Object[][] data = new Object[patchsplit.length][1];
    for (int x = 0; x < patchsplit.length; x++) {
        data[x][0] = patchsplit[x];

    }
    return data;
}

Upvotes: 2

Related Questions