junitjunit-runner

JUnit - must return a Collection of arrays error


public class EmployeeDetails {

    private String name;
    private double monthlySalary;
    private int age;

    /**
     * @return the name
     */
    public String getName() {
            return name;
        }
        /**
         * @param name the name to set
         */
    public void setName(String name) {
            this.name = name;
        }
        /**
         * @return the monthlySalary
         */
    public double getMonthlySalary() {
            return monthlySalary;
        }
        /**
         * @param monthlySalary the monthlySalary to set
         */
    public void setMonthlySalary(double monthlySalary) {
            this.monthlySalary = monthlySalary;
        }
        /**
         * @return the age
         */
    public int getAge() {
            return age;
        }
        /**
         * @param age the age to set
         */
    public void setAge(int age) {
        this.age = age;
    }
}

How to pass the list of EmployeeDetails.class to the JUnit parameterized class.

Please help me on writing the Parameters method

@Parameters
public static Collection employeeList()
{
    List<EmployeeDetails> employees = new ArrayList<EmployeeDetails>;
    return employees;
}

// This throws error like "employeeList must return a Collection of arrays."

EmployeeDetails class above is for an example. I need to use it for a similar class where i will send the list of the class objects.


Solution

  • Your @Parameters method must return a collection of object arrays. So assuming your test case constructor just expects one EmployeeDetails object, do this:

    @Parameters
    public static Collection<Object[]> employeeList() {
        List<EmployeeDetails> employees = new ArrayList<>();
        // fill this list
    
        Collection<Object[]> result = new ArrayList<>();
        for (EmployeeDetails e : employees) {
            result.add(new Object[] { e });
        }
        return result;
    }