TestNG 方法中的可变长度参数 [英] Variable length arguments in TestNG Method
本文介绍了TestNG 方法中的可变长度参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我一直在用 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
}
}
这段代码给了我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?
推荐答案
请尝试使用正确的数组初始化,它应该可以工作.我只用正确的数组初始化尝试了相同的代码,并且成功了.
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);
}
同时附上 TestNG 结果...
Also attaching the TestNG results...
希望能帮到你
这篇关于TestNG 方法中的可变长度参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文