2015-04-29 29 views
3

我的代码:GetRequest的()必须返回数组的可迭代

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

回答

3

对于参数化测试,JUnit的通过了测试参数,测试类的构造函数。由于构造函数可以使用多个参数,因此JUnit希望每个参数集都是一个数组。数组的元素必须符合构造函数参数。

因此,您的配置方法必须返回数组的Iterable,例如, Collection<Object[]>。在你的情况,你就必须每次运行一个单一的参数,所以你的数组将有长度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

+0

请参阅我的更新。它仍然不起作用。 –

+0

@EladBenda好的,我从内存编码 - 可能返回的集合必须是类型“集合”。我会检查的。 – isnot2bad

+0

@EladBenda修复了代码。参数集的类型必须是Object []'。 – isnot2bad

5

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

查看JUnit 4.12 release notes了解更多详情。

+0

谢谢 - 这是我的观点。我用我的groovy - > lib文件夹中的junit-4.12.jar替换了junit-4.11.jar,并且我的参数化测试按预期工作。 – rplantiko

+0

很棒的回答。我从一个运行良好的同事那里得到了这段代码,所以当我得到错误 –

+0

OMG时,我有点惊讶,我一直在用这个方法上去。我的POM说Junit 4.11。现在修好! –