Q:

How to read multiple test data files in same script?

I'm trying to use data in multiple test data files in the same script here. Suppose there are 2 data files with login credentials (Login1.csv and Login2.csv).
I need to use credentials in Login1.csv in one environment (on-premise) and credentials in Login2.csv in another environment (cloud) and need to use data in both files in the same script. I have created 2 test cases there (test1 and test2) but I have about 50 test cases in the real project. 
The script below is working fine for the first csv file (Login1.csv) but I'm not sure if it's possible to use credentials in Login2.csv in same script (in test1 and test2).
public class Test_Login2 {

    @Test(dataProvider = "dp")
    public void test1(HashMap<String, String> b) {
        System.out.println(b.get("Username"));
        System.out.println(b.get("Password"));
        System.out.println("*************************");
        // -------------other scripts----------------
    }

    @Test(dataProvider = "dp")
    public void test2(HashMap<String, String> b) {
        System.out.println(b.get("Username"));
        System.out.println(b.get("Password"));
        System.out.println("*************************");
        // -------------other scripts----------------
    }

    @DataProvider(name = "dp")
    public Object[][] dpmethod() throws Exception {

        // test data file for one environment (on-premise)
        String csvfile = "C:\\Users\\.....\\Login1.csv";
        CSVReader reader1 = new CSVReader(new FileReader(csvfile));
        //----------script for reading data in login1 file

        // test data file for another environment (cloud)    
        String csvfile = "C:\\Users\\.....\\Login2.csv";
        CSVReader reader2 = new CSVReader(new FileReader(csvfile));
        //----------script for reading data in login2 file
    }
}

A:

You can pass the environment through command line using the -DEnv="cloud" option and read it in your code.
Based on the environment, you can use a particular filepath to generate the dataprovider and pass it to your test cases.
@DataProvider(name="dp")
public Object[][] dpmethod()throws Exception{
   // Reading the environment variable here.
   String env = System.getProperty("Env");
   String csvFile = null;
   if(env.equals("on-premise")){
      // test data file for one environment (on-premise)
      csvFile = "C:\\Users\\.....\\Login1.csv";
   }else if(env.equals("cloud")){
      //test data file for another environment (cloud)
      csvfile = "C:\\Users\\.....\\Login2.csv";
   }else{
      System.out.println("Unsupported environment.");
      System.exit(0);
   }
   CSVReader reader = new CSVReader(new FileReader(csvfile));
   // code for reading data 
   // from one of the files 
   // and create the data object

   // return the data object created above.
}

