JUnitParamsRunner - 无法将变量参数或对象数组传递给 test()?

JUnitParamsRunner - cannot pass variable parameters or array of Objects to the test()?

我发现我无法声明 public void test(Obj...objects) 并使用 JUnitParamsRunner 来参数化我的测试..在运行时抛出异常。 但是,如果我将其更改为 public void test(Obj obj1, Obj obj2),它会正常工作 任何的想法?下面是代码:

private static Object[] testingParam() {
  return new Object[] { new Object[] { new Obj("123"), new Obj("123") } ];
}    

@Test
@Parameters(method = "testingParam")    
public void test(Obj...objects){
  //do some test
}

虽然我没有机会对此进行测试,但看来 based on the usage documentation 您可能需要 Obj[] 数组而不是 Object[] 数组来匹配可变参数。请注意,Object[]Obj[] 不是协变的,不能相互转换。

private static Object[] testingParam() {
  return new Object[] { new Obj[] { new Obj("123"), new Obj("123") } };
}

相反,如果您试图忽略可变参数并将参数视为原始数组,您可能需要第三个数组包装器:

private static Object[] testingParam() {
  return new Object[] {  // <-- call the testing method once
      new Object[] {     // <-- with this array of parameters
          new Obj[] {    // <-- and the first parameter is a 2-element Obj array
              new Obj("123"), new Obj("123") } } };
}