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

TestNG data provider with folder contents using lambda

我想通过一个 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)tst\resources\json\admin.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() })