GetRequests () must return Iterable arrays

my code:

@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);                                                                
    }                                                                                                      

      

returns the following exception:

getRequests() must return an Iterable of arrays

how can I run a parameterized junit with increment int

only as an input parameter?

let's say run the same tests for i=0 ...100

?

Update

I tried

//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;
}

      

and still get the error:

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)

      

+3


source to share


2 answers


For parameterized tests, JUnit passed test parameters to the test class constructor. Since a constructor can take more than one argument, JUnit expects each parameter to be an array. Array elements must match constructor arguments.

So your config method should return Iterable

arrays eg. Collection<Object[]>

... In your case, you only have one parameter per run, so your array will be 1 in length:



@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;
}     

      

Also note that your setup method should never be initialized as your method seems to think! Initialization is performed only in @Before

or @BeforeClass

!

+3


source


Junit 4.12+ no longer has this limitation. So if you develop your tests with JUnit 4.12+ and then run those tests with 4.11, you will also get this error message.



See the JUnit 4.12 release notes for details .

+7


source







All Articles