Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Variable length arguments in TestNG Method

Tags:

java

testng

I have been writing the tests in TestNG. My aim is to provide variable length arguments to the test method. I am facing a problem which is not making any sense to me. The codes are as follows:

@DataProvider(name = "testData")
public static Object[][] testDataProvider() {
    return new Object[][] {
        {
            "hello",
            "bye"
        }
        {
            "hey"
        }
    };
}

@Test(dataProvider = "testData", DataProviderClass = TestData.class)
public void test(String... strings) {
    if (strings.length == 1) {
        // do something
    }
    else {
        // do something
    }
}

This code gives me IllegalArgumentException.

  1 java.lang.IllegalArgumentException: argument type mismatch
  2     at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
  3     at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
  4     at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
  5     at java.lang.reflect.Method.invoke(Method.java:606)
  6     at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:84)
  7     at org.testng.internal.Invoker.invokeMethod(Invoker.java:714)
  8     at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:901)
  9     at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1231)
 10     at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:127)
 11     at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:111)
 12     at org.testng.TestRunner.privateRun(TestRunner.java:767)
 13     at org.testng.TestRunner.run(TestRunner.java:617)
 14     at org.testng.SuiteRunner.runTest(SuiteRunner.java:334)
 15     at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:329)
 16     at org.testng.SuiteRunner.privateRun(SuiteRunner.java:291)
 17     at org.testng.SuiteRunner.run(SuiteRunner.java:240)
 18     at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
 19     at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
 20     at org.testng.TestNG.runSuitesSequentially(TestNG.java:1224)
 21     at org.testng.TestNG.runSuitesLocally(TestNG.java:1149)
 22     at org.testng.TestNG.run(TestNG.java:1057)
 23     at org.testng.remote.RemoteTestNG.run(RemoteTestNG.java:111)
 24     at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:204)
 25     at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:175)

Could somebody please tell me why is this error coming and how can I resolve the issue?

like image 841
Vaibhav Agarwal Avatar asked Feb 25 '14 07:02

Vaibhav Agarwal


People also ask

What is a variable length argument list?

Variable-length argument lists, makes it possible to write a method that accepts any number of arguments when it is called. For example, suppose we need to write a method named sum that can accept any number of int values and then return the sum of those values.

How can variable length arguments be helpful?

Variable length arguments are most useful when the number of arguments to be passed to the method is not known beforehand. They also reduce the code as overloaded methods are not required.

What is @parameter annotation in TestNG?

Parameters Annotation in TestNG is a method used to pass values to the test methods as arguments using . xml file. Users may be required to pass the values to the test methods during run time. The @Parameters annotation method can be used in any method having @Test, @Before, @After or @Factory annotation.

What is the difference between DataProvider and parameter in TestNG?

What is the difference between DataProvider and Parameter in TestNG? DataProviders pass the different parameters on a single test in a single execution, whereas parameters pass the parameters just once per execution in TestNG.


1 Answers

Please try to use proper array initialization and it should work. I tried the same code just with correct array initialization and it worked.

@DataProvider(name = "testData")
    public static Object[][] testDataProvider() {
        return new Object[][] {
            new String[]{ "abc", "abcd" },
            new String[]{ "abc", "abcd", "123" }
        };
    }

    @Test(dataProvider = "testData")
    public void test(String... str) {
        System.out.println("The Length is" + str.length);

    }

Also attaching the TestNG results... enter image description here

Hope this helps

like image 127
Anuragh27crony Avatar answered Sep 28 '22 06:09

Anuragh27crony