【发布时间】:2018-04-19 18:27:44
【问题描述】:
这是我要测试的代码
public class ChargeModule {
private int quantityPrn;
private double totalCharge;
private ArrayList<boolean[]> details;
public ChargeModule(int quantityPrn, ArrayList<boolean[]> details) {
this.quantityPrn = quantityPrn;
this.details = details;
totalCharge = 0.0;
}
public double getTotalCharge() {
return totalCharge;
}
public int quantityPrn() {
return quantityPrn;
}
public void chargeCalculation() {
if(quantityPrn < 1 || quantityPrn > 100)
throw new IllegalArgumentException();
double additionalCharge = 0.00;
double charge = 0.00;
double addCharge = 0.00;
if(quantityPrn < 5)
charge = 1.00;
else if(quantityPrn < 10)
charge = 0.90;
else if(quantityPrn < 20)
charge = 0.70;
else if(quantityPrn < 50)
charge = 0.50;
else if(quantityPrn < 100)
charge = 0.10;
else
charge = 0.10;
for (int i = 0; i < details.size() ; i++) {
boolean[] detail = (boolean[])details.get(i);
boolean hqp = detail[0];
boolean de = detail[1];
if(hqp && de)
addCharge = 0.20;
else if(!hqp && !de)
addCharge = 0.00;
else
addCharge = 0.10;
additionalCharge += addCharge;
}
charge *= quantityPrn;
totalCharge = additionalCharge + charge;
}
这是单元测试的代码
public class ChargeModuleUnitTest {
ChargeModule cm;
Scanner inputStream;
@Before
public void initialization() {
String fileName = "testData.txt";
inputStream = null;
try {
inputStream = new Scanner(new File(fileName));
}
catch (FileNotFoundException e) {
System.out.println(e);
}
}
public Object[] getFileToTestTotalCharge() {
ArrayList<boolean[]> testData;
Object[] allTestData = new Object[2];
String details;
int quantity;
double expectedValue = 0.0;
int tt,tf,ff,ft,x=0;
boolean[] TT = new boolean[] {true,true};
boolean[] TF = new boolean[] {true,false};
boolean[] FF = new boolean[] {false,false};
boolean[] FT = new boolean[] {false,true};
while(inputStream.hasNextInt()) {
testData = new ArrayList<boolean[]>();
quantity = Integer.parseInt(inputStream.nextLine());
if(inputStream.hasNextLine()) {
details = inputStream.nextLine();
String[] tokens = details.split(" ");
tt = Integer.parseInt(tokens[0]);
tf = Integer.parseInt(tokens[1]);
ff = Integer.parseInt(tokens[2]);
ft = Integer.parseInt(tokens[3]);
for(int i = 0; i < tt ; i++)
testData.add(TT);
for(int j = 0; j < tf ; j++)
testData.add(TF);
for(int k = 0; k < ff ; k++)
testData.add(FF);
for(int l = 0; l < ft ; l++)
testData.add(FT);
}
if(inputStream.hasNextDouble())
expectedValue = Double.parseDouble(inputStream.nextLine());
allTestData[x] = new Object[] {quantity,testData,expectedValue};
x++;
}
inputStream.close();
return allTestData;
}
@Test
@Parameters(method = "getFileToTestTotalCharge")
public void testing(int quantity, ArrayList<boolean[]> details, double expectedValue){
cm = new ChargeModule(quantity,details);
cm.chargeCalculation();
assertEquals(expectedValue,cm.getTotalCharge(),0.01);
}
@Test(expected = IllegalArgumentException.class)
public void testasd() {
cm = new ChargeModule(-1,null) ;
cm.chargeCalculation();
}
}
这是我的 testData 文件 testData.txt
这就是错误的样子 Error Picture
我很困惑为什么它是初始化错误,因为所有代码都没有问题我很困惑也许参数有错误但我不知道它在哪里希望有人可以帮助我解决这个问题。
【问题讨论】:
-
你能说明你在哪里调用 getfileToTestTotalCharge() 吗?
-
junit 的哪个版本? JUnit4 有 @Parameter 但没有方法参数,而且它没有在测试方法中使用,而是在返回参数的函数中使用。 JUnit5 似乎也没有这个类
-
我正在使用junit5
-
并且我还添加了外部路径以具有参数测试功能
-
@BearSio,是什么让你认为你在使用“JUnit 5”?
标签: java eclipse unit-testing junit junit5