0
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.

Rajagopal
  • 931
  • 1
  • 8
  • 23

3 Answers3

3

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;
}
Duncan Jones
  • 67,400
  • 29
  • 193
  • 254
0

If you can use static constructors, you can be a lot terser in your setup

@Parameterized.Parameters
public static Collection<Object[]> params() {
  return Arrays.asList(new Object[][] {
      { 0, "a" },
      { 1, "b" },
      { 3, "c" },
  });
}
Tunaki
  • 132,869
  • 46
  • 340
  • 423
stevel
  • 12,567
  • 1
  • 39
  • 50
-2

Collection is a generic type you must specify the type of the returned Collection<E> where E is the type, here it must be EmployeeDetails

your code must look like this:

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

or simply

public static Collection<?> employeeList()
{
List<EmployeeDetails> employees = new ArrayList<EmployeeDetails>();
return employees;
}
monim
  • 3,427
  • 3
  • 23
  • 36
  • why downvoting? please leave comments – monim Aug 27 '14 at 06:49
  • Your answer is incorrect - it's essentially the same code as the OP already has, but just adds Java generics - it doesn't solve the problem that the return type of `employeeList()` is incorrect. See the accepted answer. – Krease Jan 19 '15 at 23:21