0
\$\begingroup\$

I want to convert a list of integers into a two-dimensional array for use with TestNG DataProvider. Here is the code I've written:

static List<Integer> studentList = new ArrayList<Integer>();

@Test()
public void testMethod(){
    studentList.add(477915);
    studentList.add(477916);
    studentList.add(477917);
    System.err.println(studentList);
}

@DataProvider(name = "persons")
public static Object[][] primeNumbers() {
    Object [][] objArray = new Object[studentList.size()][];

    for(int i=0;i< studentList.size();i++){
        objArray[i] = new Object[1];
        objArray[i][0] = studentList.get(i);
    } 

    return objArray;
}

@Test(dataProvider="persons")
public void testRunPersonRules(int val) {
    System.err.println(val);
}

Is there a better way to accomplish this?

\$\endgroup\$
1
  • \$\begingroup\$ Not an answer, but do look at spock framework for data driven testing. They have nice constructs for this case. \$\endgroup\$ Commented Apr 13, 2016 at 18:46

1 Answer 1

6
\$\begingroup\$

You can use streams:

Object[][] data = studentList.stream()
        .map(studentId -> new Object[]{studentId})
        .toArray(Object[][]::new);
\$\endgroup\$

You must log in to answer this question.