Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/399.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 转换列表<;T>;收集<;对象[]>;对于JUnit参数化测试_Java_Arrays_Junit_Parameterized - Fatal编程技术网

Java 转换列表<;T>;收集<;对象[]>;对于JUnit参数化测试

Java 转换列表<;T>;收集<;对象[]>;对于JUnit参数化测试,java,arrays,junit,parameterized,Java,Arrays,Junit,Parameterized,我想使用外部数据执行JUnit参数化测试。 我有一个对象列表,只需要知道如何将其转换为对象数组集合。我看到了下面的堆栈溢出问题,但我想添加从我的方法读取的文件中的数据 工作代码:类似以下内容: @RunWith(Parameterized.class) public class sampletest { private BranchMailChildSample branch; public sampletest(BranchMailChildSample branch) {

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

工作代码:类似以下内容:

@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());
  }
}
@RunWith(参数化的.class)
公共类抽样测试{
私人分行mailchildsample分行;
公共抽样测试(BranchMailChildSample分支)
{
this.branch=分支;
}
@参数
公共静态收集数据()
{
String excel=“C:\\Resources\\TestData\\ExcelSheets\\BranchMail\\BranchMail\u TestData.xlsx”;
ExcelMarshallerTool=新的ExcelMarshallerTool(excel);
列表项=tool.unmarshallExcel(BranchMailChildSample.class);
//在这里我需要帮助:将列表转换为集合
//将项作为对象数组的集合返回
}
@试验
公开无效测试()
{
System.out.println(branch.toString());
}
}

这就是我建议您尽可能使用的原因。 它的工作原理与JUnit相同,但您可以使用内部或外部数据提供程序,以给定的顺序执行方法。。。使用起来方便多了

//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);
}
事实上,如果你使用和Theory runner,类似的东西应该会起作用:

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

   @Theory
   public void canDoTheThing(MyClass m) throws Exception {
@RunWith(理论类)
公共类MyTest
{
@DataPoints公共静态列表myFunctionThatReturnsTestData()
{
//待办事项
}
@理论
公共void canDoTheThing(MyClass m)引发异常{

我让它开始这样做:

    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;
List items=tool.unmarshallExcel(BranchMailChildSample.class);
收集数据=新的ArrayList();
对于(BranchMailChild示例项:项)
{
Object[]objItem=新对象[]{item};
data.add(objItem);
}
返回数据;

您不必转换列表

@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());
  }
}
@RunWith(参数化的.class)
公共类抽样测试{
@参数(name=“{0}”)
公共静态列表数据(){
String excel=“C:\\Resources\\TestData\\ExcelSheets\\BranchMail\\BranchMail\u TestData.xlsx”;
ExcelMarshallerTool=新的ExcelMarshallerTool(excel);
返回工具.unmarshallExcel(BranchMailChildSample.class);
}
@参数(0)
公共分行mailchildsample分行;
@试验
公开无效测试(){
System.out.println(branch.toString());
}
}
我使用了字段注入,因为它比构造函数注入需要更少的代码。将名称设置为被测对象会打印出更有用的输出。请查看

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

@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());
  }
}
@RunWith(参数化的.class)
公共类抽样测试{
@参数(name=“{0}”)
公共静态列表数据(){
String excel=“C:\\Resources\\TestData\\ExcelSheets\\BranchMail\\BranchMail\u TestData.xlsx”;
ExcelMarshallerTool=新的ExcelMarshallerTool(excel);
返回工具.unmarshallExcel(BranchMailChildSample.class);
}
私人最终分行mailchildsample分行;
公共抽样测试(BranchMailChildSample分支){
this.branch=分支;
}
@试验
公开无效测试(){
System.out.println(branch.toString());
}
}

最好创建预期的
列表并测试相关代码。.我可以这样做,但我希望在junit插件中将其显示为多个测试。如果列表中的一个对象失败,则整个测试将失败。调试将花费更多时间,而不是我可以获得每个da的快照ta失败。您可以将列表声明为类的一个字段,初始化它并在
@Before
方法中填充它,然后根据需要使用它。它与使用其他框架时不同,但它是一个选项。我可以这样做,但这并不理想。我希望它在运行程序中显示参数化测试的外观,并且我需要能够在bi时看到它rds eye view,哪些数据集失败。我最初使用的是这个名为Feed4Junit的库,但后来发现,它与JUnit 4.12 databene.org/feed4junitPlease不兼容,请至少提供如何使用TestNG实现这一点,否则它应该是一个注释。我在anserw中提供了如何使用它的链接:不可能e、 我想使用TestNG,但我们被JUnit困住了,因为这是已经被利用的。好吧,这是一个问题,但就信息而言,TestNG也可以运行JUnit测试。虽然这会起作用,但我仍然会遇到问题。第一个问题是,JUnit插件运行程序将它作为单个测试进行报告。我希望每个数据段都显示为一个测试不同的测试,因此我可以鸟瞰通过的测试和失败的测试。其次,在使用理论时,如果我使用断言,它将拖拽测试而不运行其他测试。如果测试失败,使用Assums将在运行程序中将测试标记为通过,因此我也不想使用它。我最初使用的是名为Feed4Junit的库,但后来发现事实上,JUnit 4.12不能很好地使用。谢谢,这非常有用。我甚至不知道你可以这样做。我想,我唯一不喜欢的是,我的测试数据必须以这种方式公开。但这应该不是一个太大的问题。这是因为参数化的运行程序需要访问数据()方法。我指的是属性分支。它不能使用私有值来设置它,甚至不能使用setter。你可以使用构造函数注入。我添加了一个额外的示例。哦,好的,谢谢,我觉得它必须是一个集合,其类型为对象数组。谢谢