gpt4 book ai didi

java - JUnit 出现初始化错误( eclipse 氧气)

转载 作者:太空宇宙 更新时间:2023-11-04 10:31:39 24 4
gpt4 key购买 nike

这是我要测试的代码

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.txt

这就是错误的样子 Error Picture

我很困惑为什么会出现初始化错误,因为所有代码都没有问题,我很困惑可能参数有错误,但我不知道它在哪里,希望有人可以帮助我解决这个问题。

最佳答案

首先,由于某种原因,@Parameter功能不适用于@Before@BeforeClass(我需要进一步研究,但调试时从未到达断点)。

因此,之后,我们必须记住,我们的参数必须以某种方式在类上指定,这样 @Parameters 方法就可以在每个循环中获取它们。一旦我们使用多个参数,我们必须指定哪个是该参数的索引。

我还观察到读取 double /浮点值时无法正常工作,因此我更改了文件以替换“。”到“,”(例如:4.40 变成 4,40)。

最后但并非最不重要的一点是,当我们使用@Parameterized运行器时,我们必须在该测试用例上保留参数化测试。

所以,对我来说,最终结果是这样的:

import static org.junit.Assert.assertEquals;

import java.io.File;
import java.io.FileNotFoundException;
import java.text.NumberFormat;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Scanner;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameter;
import org.junit.runners.Parameterized.Parameters;

@RunWith(Parameterized.class)
public class ChargeModuleUnitTest {

@Parameter(0)
public static int quantity;

@Parameter(1)
public static ArrayList<boolean[]> testData;

@Parameter(2)
public static double expectedValue;

public ChargeModule cm;


@Parameters(name = "{index}: quantity = {0}, testData = {1}, expectedValue = {2}")
public static Object[] getFileToTestTotalCharge() {
String fileName = "C:\\Users\\Eduardo Meneses\\workspace\\StackOverflow\\src\\testData.txt";
Scanner inputStream = null;
try {
inputStream = new Scanner(new File(fileName));
} catch (FileNotFoundException e) {
System.out.println(e);
}

Object[] allTestData = new Object[2];
String details;

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()){
try {
expectedValue = NumberFormat.getInstance().parse(inputStream.nextLine()).doubleValue();
} catch (ParseException e) {
e.printStackTrace();
}
}

allTestData[x] = new Object[] { quantity, testData, expectedValue };
x++;
}

inputStream.close();
return allTestData;
}

@Test
public void testing() {
cm = new ChargeModule(quantity, testData);
cm.chargeCalculation();
assertEquals(expectedValue, cm.getTotalCharge(), 0.01);
}
}

按照文档进行参数化测试:https://github.com/junit-team/junit4/wiki/Parameterized-tests

此外,this是此类测试的一个很好的例子。

PS:你上次的测试失败了。

关于java - JUnit 出现初始化错误( eclipse 氧气),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49927833/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com