将List <t>转换为Collection <object []>以获取JUnit参数化测试</object []> </t>

时间:2015-04-14 16:07:47

标签: java arrays junit parameterized

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

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

4 个答案:

答案 0 :(得分:2)

您不必转换列表。

@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

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

@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());
  }
}

答案 1 :(得分:0)

这就是为什么我建议你尽可能使用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);
}

答案 2 :(得分:0)

实际上,如果你使用JUnit 4.12和理论运动员,这样的事情应该可行:

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

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

答案 3 :(得分:0)

我做到了这一点:

    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;