【问题标题】:getRequests() must return an Iterable of arraysgetRequests() 必须返回一个 Iterable 数组
【发布时间】:2015-04-29 17:50:21
【问题描述】:

我的代码:

@RunWith(Parameterized.class)                                                                              
public class FreshResultCompareRunner2 {                                                                   


    //This is called before @BeforeClass !                                                                 
    @Parameterized.Parameters                                                                              
    public static Collection getRequests() throws IOException {                                            
        injector = Guice.createInjector(new MainModule());                                                 
        initStaticFromInjector();                                                                          
        initTestInput();                                                                                   
        return OrganizeTestParameterizedInput();                                                           
    }                                                                                                      


    private static void initTestInput() throws IOException {                                               

    }                                                                                                      

    private static Collection OrganizeTestParameterizedInput() {                                           

        Object[] objectMatrix = new Object[100];                                                
        for (int i = 0; i < 100; i++) {                                                         
            objectMatrix[i] = i;                                                                           
        }                                                                                                  
        return Arrays.asList(objectMatrix);                                                                
    }                                                                                                      

返回以下异常:

getRequests() must return an Iterable of arrays

如何运行参数化的junit,增加int 仅作为输入参数?

说为i=0 ...100 运行相同的测试?

更新

我试过了

//This is called before @BeforeClass !
@Parameterized.Parameters
public static Collection<int[]> getParameters() {
    injector = Guice.createInjector(new MainModule());
    initStaticFromInjector();

    int numOfChunks = 3;//routingResponseShortRepository.getNumOfBaseLineChunks();
    //might be less
    int totalResponses = numOfChunks * globalSettings.requestsChunkSize;

    Collection<int[]> params = new ArrayList<>(totalResponses);
    for(int i = 1; i <= totalResponses; ++i) {
        params.add(new int[] { i });
    }
    return params;
}

//takes the next matrix row from OrganizeTestParameterizedInput()
public FreshResultCompareRunner2(int responseId) {
    this.responseId = responseId;
}

还是报错:

java.lang.Exception: com.waze.routing.automation.runners.FreshResultCompareRunner2.getParameters() must return an Iterable of arrays.
    at org.junit.runners.Parameterized.parametersMethodReturnedWrongType(Parameterized.java:343)

【问题讨论】:

    标签: java unit-testing testing junit parameterized


    【解决方案1】:

    Junit 4.12+ 不再有这个限制。因此,如果您使用 JUnit 4.12+ 开发测试,然后使用 4.11- 执行这些测试,您也会收到此错误消息。

    更多详情请见JUnit 4.12 release notes

    【讨论】:

    • 谢谢 - 这是我的观点。我在我的 groovy -> lib 文件夹中用 junit-4.12.jar 替换了 junit-4.11.jar,并且我的参数化测试按预期工作。
    • 很好的答案。我从一位运行良好的同事那里得到了这段代码,所以当我收到错误时我有点惊讶
    • 天啊,我一直在用这个爬墙。我的 POM 说 Junit 4.11。现已修复!
    【解决方案2】:

    对于参数化测试,JUnit 将测试参数传递给测试类的构造函数。因为构造函数可以接受多个参数,所以 JUnit 期望每个参数集都是一个数组。数组的元素必须符合构造函数的参数。

    因此,您的配置方法必须返回数组的Iterable,例如Collection&lt;Object[]&gt;。在您的情况下,每次运行只有一个参数,因此您的数组的长度为 1:

    @Parameterized.Parameters                                                                              
    public static Collection<Object[]> getParameters() {                                            
        Collection<Object[]> params = new ArrayList<>(100);
        for(int i = 1; i <= 100; ++i) {
            params.add(new Object[] { i });
        }
        return params;
    }     
    

    还请注意,您的配置方法不应该像您的方法那样做任何初始化!初始化仅在@Before@BeforeClass 中完成!

    【讨论】:

    • @EladBenda 好的,我从内存中编写了这个 - 可能返回的集合必须是 Collection&lt;Object[]&gt; 类型。我会检查的。
    • @EladBenda 修复了代码。参数集的类型必须为Object[]
    猜你喜欢
    • 2016-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-25
    • 2021-07-21
    • 1970-01-01
    • 2021-07-06
    相关资源
    最近更新 更多