簡體   English   中英

參數化的JUnit + JUnitParams測試不適用於帶有

[英]Parametrized JUnit+JUnitParams test is not working for arrays with

您能告訴我為什么測試不起作用嗎? 我有 :

java.lang.IllegalStateException:嘗試創建類類[Ljava.lang.Integer; 找不到參數與類型相匹配的構造函數。

而且我找不到一個示例,其中JUnitParamsRunner使用數組作為參數。

@RunWith(JUnitParamsRunner.class)
public class StatisticsUtilsParameterizedTest {

    private Object[] getValues() {
        Object[] objects = new Object[2];
        objects[0] = new Integer[]{1, 2, 3};
        objects[1] = 2;
        return objects;
    }

    @Test
    @Parameters(method = "getValues")
    public void shouldCalcAverageOK(Integer[] arg, int expected) {
        int average = StatisticsUtils.getAverage(arg);//requires an array
        assertEquals(expected, average);
    }
}

有沒有辦法使其與JUnitParams一起使用?

嘗試這個:

private Object[] getValues() {
    return $(
                 $($(1,2,3), 2),
                 $($(2,3,4), 4)
            );
}

或以您嘗試撰寫的方式

  private Object[] getValues() {
      Object[] objects = new Object[2];
      objects[0] = new Object[]{new Integer[]{1, 2, 3},2};
      objects[1] = new Object[]{new Integer[]{2, 3, 4},4};
      return objects;
  }

希望這可以幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM