我想通过 TestNG 数据提供程序给出文件名列表,以便测试可以加载每个文件。
Object[][] result = Files.list(Paths.get("tst/resources/json"))
.filter(Files::isRegularFile)
.map(fileName -> new Object[] { fileName })
.toArray(Object[][]::new);
我已经到了可以从文件夹内容构建 Object[][] 的地步,但 TestNG 抛出异常:
org.testng.internal.reflect.MethodMatcherException:
Data provider mismatch
Method: testFBTinka11InterpretJson([Parameter{index=0,
type=java.lang.String, declaredAnnotations=[]}])
Arguments: [(sun.nio.fs.WindowsPath$WindowsPathWithAttributes)tstresourcesjsonadmin.json]
at org.testng.internal.reflect.DataProviderMethodMatcher.getConformingArguments(DataProviderMethodMatcher.java:52)
。
在我看来,使用数据提供程序的@Test
方法仅接受文件名作为String
但数据提供程序实际上为其提供了一个File
对象,这就是它中断的地方。
您有两种选择:
- 将
@Test
方法更改为接受File
对象而不是String
。(或( - 您将数据提供程序更改为开始仅提供
File
对象的绝对路径,而不是File
对象。 即将.map(fileName -> new Object[] { fileName })
更改为.map(fileName -> new Object[] { fileName.getAbsolutePath() })