title: How can I use an Object iterator DataProvider and run multiple tests in parallel in java?
body: <p>So I need to check multiple CSVs (more than 500k) and the number is never the same. Each CSV file has one or more lines, and 30 rows. I need to take each file, and validate the fields whit data from an API.</p>
  
  <p>The solution I am thinking now is:</p>
  
  <p>One DataProvider that can provide an Object iterator with all the files I need to check.
  For each file I will execute multiple tests(10-15).
  All run in parallel with multiple threads.
  And to not execute the API request for each test I need to make the request @BeforeSuite.</p>
  
  <p>I didn't find any way to execute the tests in parallel with this type of DataProvider. The tests run first test1 for all the data set, then test2 for all the data set, so making the API call @BeforeSuite has no value. Any idea?</p>
  
  <p>This is a code example:</p>
  
  <p>Data factory:</p>
  
  <pre><code>import org.testng.annotations.DataProvider;
  import org.testng.annotations.Factory;
  
  import java.io.File;
  import java.util.ArrayList;
  import java.util.Iterator;
  import java.util.List;
  
  public class DataFactory {
      @Factory(dataProvider = "dp")
      public Object[] createInstances(String filename) {
          return new Object[]{new TestSuite(filename)};
      }
  
      private static final String CSV_PATH = "src/main/resources/files/";
  
      @DataProvider(name = "dp")
      public Iterator&lt;Object[]&gt; getFileList() {
          File[] files = new File(CSV_PATH).listFiles();
          List&lt;Object[]&gt; data = new ArrayList&lt;&gt;();
  
          for (File file : files) {
              data.add(new String[]{file.getName()});
          }
  
          return data.iterator();
      }
  }
  </code></pre>
  
  <p>Tests class:</p>
  
  <pre><code>import java.lang.reflect.Method;
  import org.testng.annotations.Test;
  
  public class TestSuite {
      String filename1 = "";
  
      public TestSuite(String filename) {
  
          filename1 = filename;
      }
  
      @Test
      public void test1(Method res) {
          System.out.println("…Executing…" + res.getName() + " dataProviderData " + filename1 + " thread number " + Thread.currentThread().getId());
      }
  
      @Test
      public void test2(Method res) {
          System.out.println("…Executing…" + res.getName() + " dataProviderData " + filename1 + " thread number " + Thread.currentThread().getId());
      }
  </code></pre>
  
  <p>TestNG XML config:</p>
  
  <pre><code>&lt;suite name="Tests Suite" thread-count="20" parallel="methods"&gt;
      &lt;test name="CSV test"&gt;
          &lt;classes&gt;
              &lt;class name="DataFactory" group-by-instances="true"/&gt;
          &lt;/classes&gt;
      &lt;/test&gt;
  &lt;/suite&gt;
  </code></pre>