使用 lambda 测试具有文件夹内容的 TestNG 数据提供程序



我想通过 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对象,这就是它中断的地方。

您有两种选择:

  1. @Test方法更改为接受File对象而不是String。(或(
  2. 您将数据提供程序更改为开始仅提供File对象的绝对路径,而不是File对象。 即将.map(fileName -> new Object[] { fileName })更改为.map(fileName -> new Object[] { fileName.getAbsolutePath() })

最新更新