首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Junit @RunWith(Parameterized.class)

Junit @RunWith(Parameterized.class)
EN

Stack Overflow用户
提问于 2016-05-15 03:31:41
回答 2查看 1.7K关注 0票数 0

以下两种情况的区别是

a>变量'arr‘的类型。在第一种情况下,它的类型是Integer[],而在第二种情况下,它的类型是int[]。

我们可以从我标记为"// b>“的地方看到,在案例1中,它需要Integer[]。而在第二种情况下,它需要int。

情况1和情况2都有效。所以我的问题来了:

当测试数据是一维整数数组时,为什么Junit需要在data()方法中返回一个三维整数数组?我认为它应该是二维整数数组,因为在第二种情况下,我在data()方法中返回了一个二维整数数组,它很容易理解,而且它可以工作。但是当我试图在第一种情况下的data()方法中返回二维整数数组时

代码语言:javascript
复制
    @Parameterized.Parameters(name = "test with {index}")
    public static Iterable<Integer[]> data() {
      return Arrays.asList(new Integer[][]{ 
            {3, 1, 4, 6, 7, 9},  
            {9, 8, 7, 6, 5, 4},  
      }); 
    }

Junit报告了"java.lang.IllegalArgumentException:错误的参数数量“。如果你知道原因,请帮帮我。

我搜索了Junit文档和许多其他页面,但没有得到满意的答案。请help.My Junit版本为4.12。

案例1

代码语言:javascript
复制
@RunWith(Parameterized.class)
public class MyTest {
    @Parameterized.Parameters(name = "test with {index}")
    public static Iterable<Integer[][]> data() {
      //here:  My question is why it can not be Integer[][]
      return Arrays.asList(new Integer[][][]{ 
            {{3, 1, 4, 6, 7, 9}},  
            {{9, 8, 7, 6, 5, 4}},  
      }); 
    }

    private Integer[] arr;

    public MyTest(Integer[] arr) {
      this.arr = arr;
    }
    public methodTest(int[] arr) {
     // ignore the code here 
    }
}

案例2

代码语言:javascript
复制
@RunWith(Parameterized.class)
public class MyTest {
  @Parameterized.Parameters(name = "test with {index}")
  public static Iterable<int[]> data() {
    //here int[][] works 
    return Arrays.asList(new int[][]{ 
            {3, 1, 4, 6, 7, 9},  
            {9, 8, 7, 6, 5, 4},  
  }

  private int[] arr;

  public  MyTest(int[] arr) {
    this.arr = arr;
  }

  public methodTest(int[] arr) {
    // ignore the code here 
  }
}
EN

回答 2

Stack Overflow用户

发布于 2016-05-15 07:12:06

数据本身应该始终与构造函数所期望的数据类型相同,而不是测试所期望的数据类型。此外,您还会在data()的结构和数据本身之间迷失方向。我更喜欢下面的结构,它清楚地表达了我们需要的东西:

代码语言:javascript
复制
@Parameters
public static Collection<Object[]> data() 
{
    return Arrays.asList(
        new Object[][] 
        { 
            {/* case 1*/},
            {/* case 2*/},
        }
    );
}

也许这不是最短的方法,但你永远不会迷失它,即使是复杂的数据。除了/* case 1*//* case 1*/之外,这里的每一行代码都不会更改。在顶层,我们有一个数组(类型为object)的集合,我们通过提供一个对象的二维数组来创建这个集合,每一行表示测试用例。

所以你的Case 1会变成

代码语言:javascript
复制
@Parameterized.Parameters(name = "test with {index}")
public static Collection<Object[]> data() {
    return Arrays.asList(
        new Object[][] 
        { 
            {new Integer[]{3, 1, 4, 6, 7, 9}}, // here
            {new Integer[]{9, 8, 7, 6, 5, 4}}, // here
        }
    );
} 

而Case 2将成为

代码语言:javascript
复制
@Parameterized.Parameters(name = "test with {index}")
public static Collection<Object[]> data() {
    return Arrays.asList(
        new Object[][] 
        { 
            {new int[]{3, 1, 4, 6, 7, 9}}, // here
            {new int[]{9, 8, 7, 6, 5, 4}}, // here
        }
    );
} 

您还需要修复代码中的其他几行有问题的代码,例如,测试中缺少返回类型。下面是一个完整的runnable和通过测试的例子:

代码语言:javascript
复制
package x;

import java.util.Arrays;
import java.util.Collection;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;

@RunWith(Parameterized.class)
public class MyTest 
{
    @Parameterized.Parameters(name = "test with {index}")
    public static Collection<Object[]> data() {
        return Arrays.asList(
                new Object[][] 
                { 
                    {new Integer[]{3, 1, 4, 6, 7, 9}}, // here
                    {new Integer[]{9, 8, 7, 6, 5, 4}}, // here
      });
    }

    private Integer[] arr;

    public MyTest(Integer[] arr) {
      this.arr = arr;
    }

    @Test
    public void methodTest() {
     // test some logic
        System.out.println(arr.length);
    }
}
票数 1
EN

Stack Overflow用户

发布于 2016-05-15 07:45:53

您的Integer版本比int版本需要更多维度的原因是,第一个IterableIterable<Integer [][]>,它比第二个Iterable<int[]>多一个维度。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/37230995

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档