如何从文本文件中读取数据并将它们作为 java 中的参数化测试传递给 junit 测试?
how to read data from text file and pass them to junit test as parameterized test in java?
所以我在做 JUnit 测试。当我在文本文档中获取我的测试方法的参数时,我反驳了参数对象无法正常工作之后的问题。我的所有参数一次传递给测试方法,但我希望它们分别传递。
下图就是我想要的(红线表示第二组参数)。该图像是我的测试方法的示例运行。
这是我的代码:
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();
}
代码逻辑问题,只需将Params.add(listOfLists.toArray());
移入while循环即可解决。
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());
}
所以我在做 JUnit 测试。当我在文本文档中获取我的测试方法的参数时,我反驳了参数对象无法正常工作之后的问题。我的所有参数一次传递给测试方法,但我希望它们分别传递。
下图就是我想要的(红线表示第二组参数)。该图像是我的测试方法的示例运行。
这是我的代码:
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();
}
代码逻辑问题,只需将Params.add(listOfLists.toArray());
移入while循环即可解决。
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());
}