Search code examples
lambdajava-8testngtestng-dataprovider

TestNG data provider with folder contents using lambda


I want to give a list of file names through a TestNG data provider, so the test can load each file.

Object[][] result = Files.list(Paths.get("tst/resources/json"))
            .filter(Files::isRegularFile)
            .map(fileName -> new Object[] { fileName })
            .toArray(Object[][]::new);

I've got to the point where I can build the Object[][] from the folder contents, but TestNG throws exception:

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)

...


Solution

  • It looks to me that your @Test method which is using your data provider is accepting only file names as a String but your data provider is actually providing it with a File object and that is where its breaking.

    You have two options:

    1. You change your @Test method to accept a File object instead of String. (or)
    2. You change your data provider to start providing only absolute paths of File objects instead of File object. i.e., Change .map(fileName -> new Object[] { fileName }) to .map(fileName -> new Object[] { fileName.getAbsolutePath() })