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;
    }
}

如何将 EmployeeDetails.class 的列表传递给 JUnit 参数化类。

请帮助我编写参数方法

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

// 这会抛出类似“employeeList must return a Collection of arrays”的错误。

上面的 EmployeeDetails 类是一个例子。我需要将它用于类似的类,我将在其中发送类对象的列表。

4

3 回答 3

2

您的@Parameters方法必须返回对象数组的集合。因此,假设您的测试用例构造函数只需要一个EmployeeDetails对象,请执行以下操作:

@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;
}
于 2014-08-26T10:12:41.780 回答
0

如果您可以使用静态构造函数,您的设置会更简洁

@Parameterized.Parameters
public static Collection<Object[]> params() {
  return Arrays.asList(new Object[][] {
      { 0, "a" },
      { 1, "b" },
      { 3, "c" },
  });
}
于 2017-03-06T20:01:22.130 回答
-2

Collection是泛型类型你必须指定返回的类型Collection<E>其中E是类型,这里必须是EmployeeDetails

您的代码必须如下所示:

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

或者干脆

public static Collection<?> employeeList()
{
List<EmployeeDetails> employees = new ArrayList<EmployeeDetails>();
return employees;
}
于 2014-08-26T10:20:50.947 回答