如何从文本文件中读取数据并将其作为参数化测试传递给junit测试?

时间:2018-04-22 09:05:53

标签: java junit parameterized

所以我正在进行JUnit测试。虽然我在文本文档中获取了我的测试方法的参数,但我反击了一个问题,即在参数对象不能正常工作之后。我的所有参数都会立即传递给测试方法,但我希望它们能够单独传递。

下面的图片是我想要的(红线表示第二组参数)。该图像是我的测试方法的示例运行。

https://ibb.co/nFLhOc

这是我的代码:

    String fileName = "ChargeData.txt";

    try 
    {
        scan = new Scanner(new File(fileName));
    }
    catch(FileNotFoundException e)
    {
        e.printStackTrace();
    }

    ArrayList<Object[]> Params = new ArrayList<Object[]>();
    List listOfLists = new ArrayList();

    ArrayList <Integer> quantityList;
    ArrayList <Boolean> highQualityList;
    ArrayList <Boolean> designEffectList;
    ArrayList <Double> expectedResultList;

    while(scan.hasNext())
    {
        quantityList = new ArrayList<Integer>();
        highQualityList = new ArrayList<Boolean>();
        designEffectList = new ArrayList<Boolean>();
        expectedResultList = new ArrayList<Double>();

        while(scan.hasNextInt())
        {
            quantityList.add(new Integer(scan.next()));
        }
        for(int i=0;i<quantityList.size();i++)
        {
            highQualityList.add(new Boolean(scan.next()));
        }
        for(int i=0;i<quantityList.size();i++)
        {
            designEffectList.add(new Boolean(scan.next()));
        }
        expectedResultList.add(new Double(scan.next()));

        int[] quantity = new int[quantityList.size()];
        boolean[] highQuality = new boolean[quantityList.size()];
        boolean[] designEffect = new boolean[quantityList.size()];
        double[] expectedResult = new double[1];
        for (int i=0; i < quantity.length; i++)
        {
            quantity[i] = quantityList.get(i).intValue();
            highQuality[i] = highQualityList.get(i).booleanValue();
            designEffect[i] = designEffectList.get(i).booleanValue();
        }
        expectedResult[0] = expectedResultList.get(0).doubleValue();

        listOfLists.add(quantity);
        listOfLists.add(highQuality);
        listOfLists.add(designEffect);
        listOfLists.add(expectedResult);
    }
    Params.add(listOfLists.toArray());

    scan.close();

    return Params.toArray();
}

1 个答案:

答案 0 :(得分:0)

代码逻辑问题,只需将Params.add(listOfLists.toArray());移入,而循环将其固定。

while(scan.hasNext())
    {
        quantityList = new ArrayList<Integer>();
        highQualityList = new ArrayList<Boolean>();
        designEffectList = new ArrayList<Boolean>();
        expectedResultList = new ArrayList<Double>();

        while(scan.hasNextInt())
        {
            quantityList.add(new Integer(scan.next()));
        }
        for(int i=0;i<quantityList.size();i++)
        {
            highQualityList.add(new Boolean(scan.next()));
        }
        for(int i=0;i<quantityList.size();i++)
        {
            designEffectList.add(new Boolean(scan.next()));
        }
        expectedResultList.add(new Double(scan.next()));

        int[] quantity = new int[quantityList.size()];
        boolean[] highQuality = new boolean[quantityList.size()];
        boolean[] designEffect = new boolean[quantityList.size()];
        double[] expectedResult = new double[1];
        for (int i=0; i < quantity.length; i++)
        {
            quantity[i] = quantityList.get(i).intValue();
            highQuality[i] = highQualityList.get(i).booleanValue();
            designEffect[i] = designEffectList.get(i).booleanValue();
        }
        expectedResult[0] = expectedResultList.get(0).doubleValue();

        listOfLists.add(quantity);
        listOfLists.add(highQuality);
        listOfLists.add(designEffect);
        listOfLists.add(expectedResult);
        Params.add(listOfLists.toArray());
    }