将 List<T> 转换为 Collection<Object[]> 以进行 JUnit 参数化测试

Convert List<T> to Collection<Object[]> for JUnit Parametrized Test

我想使用外部数据执行 JUnit 参数化测试。 我有一个对象列表,只需要知道如何将其转换为对象数组的集合。我看到下面的堆栈溢出问题,但我想从从我的方法读取的文件中添加数据。

Parameterized JUnit tests with non-primitive parameters?

工作代码:像这样:

@RunWith(Parameterized.class)
public class sampletest {

  private BranchMailChildSample branch;

  public sampletest(BranchMailChildSample branch)
  {
    this.branch = branch;
  }

  @Parameters
  public static Collection<Object[]> data()
  {
    String excel = "C:\Resources\TestData\ExcelSheets\BranchMail\branchmail_TestData.xlsx";
    ExcelMarshallerTool tool = new ExcelMarshallerTool(excel);
    List<BranchMailChildSample> items = tool.unmarshallExcel(BranchMailChildSample.class);

   //RIGHT HERE I NEED HELP: Convert list to Collection<Object[]>

    //return items as Collection of object arrays 
  }

  @Test
  public void test()
  {
    System.out.println(branch.toString());
  }
}

这就是我建议您尽可能使用 TestNG 的原因。 它的工作方式与 JUnit 相同,但您可以使用内部或外部 DataProvider,按给定顺序执行方法...使用起来更方便

//This method will provide data to any test method that declares that its Data Provider
//is named "test1"
@DataProvider(name = "test1")
public Object[][] createData1() {
 return new Object[][] {
   { "Cedric", new Integer(36) },
   { "Anne", new Integer(37)},
 };
}

//This test method declares that its data should be supplied by the Data Provider
//named "test1"
@Test(dataProvider = "test1")
public void verifyData1(String n1, Integer n2) {
 System.out.println(n1 + " " + n2);
}

实际上,如果您使用 JUnit 4.12 和 theories runner,像这样的东西应该会起作用:

@RunWith(Theories.class)
public class MyTest 
{
   @DataPoints public static List<MyClass> myFunctionThatReturnsTestData() 
   {
        // TODO
   }

   @Theory
   public void canDoTheThing(MyClass m) throws Exception {

我这样做了:

    List<BranchMailChildSample> items = tool.unmarshallExcel(BranchMailChildSample.class);

    Collection<Object[]> data = new ArrayList<Object[]>();

    for(BranchMailChildSample item : items)
    {
        Object[] objItem = new Object[] { item };
        data.add(objItem);
    }

    return data;

您不必转换列表。

@RunWith(Parameterized.class)
public class SampleTest {

  @Parameters(name = "{0}")
  public static List<BranchMailChildSample> data() {
    String excel = "C:\Resources\TestData\ExcelSheets\BranchMail\branchmail_TestData.xlsx";
    ExcelMarshallerTool tool = new ExcelMarshallerTool(excel);
    return tool.unmarshallExcel(BranchMailChildSample.class);
  }

  @Parameter(0)
  public BranchMailChildSample branch;

  @Test
  public void test() {
    System.out.println(branch.toString());
  }
}

我使用了字段注入,因为它比构造函数注入需要更少的代码。将名称设置为被测对象会打印更有帮助的输出。请查看 documentation of the Parameterized runner.

如果您不喜欢 public 字段,可以使用构造函数注入。

@RunWith(Parameterized.class)
public class SampleTest {

  @Parameters(name = "{0}")
  public static List<BranchMailChildSample> data() {
    String excel = "C:\Resources\TestData\ExcelSheets\BranchMail\branchmail_TestData.xlsx";
    ExcelMarshallerTool tool = new ExcelMarshallerTool(excel);
    return tool.unmarshallExcel(BranchMailChildSample.class);
  }

  private final BranchMailChildSample branch;

  public SampleTest(BranchMailChildSample branch) {
    this.branch = branch;
  }

  @Test
  public void test() {
    System.out.println(branch.toString());
  }
}