getRequests()必须返回一个Iterable数组

时间:2015-04-29 17:50:22

标签: java unit-testing testing junit parameterized

我的代码:

@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

如何仅将int作为输入参数运行参数化junit?

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)

1 个答案:

答案 0 :(得分:9)

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

有关详细信息,请参阅JUnit 4.12 release notes