带有初始化错误的JUnit(eclipse氧气)

时间:2018-04-19 18:27:45

标签: java eclipse unit-testing junit junit5

这是我要测试的代码

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

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

1 个答案:

答案 0 :(得分:0)

首先,出于某种原因, @Parameter 功能无法使用 @Before @BeforeClass (我需要)进一步研究,但调试它从未达到过断点。

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

我还观察到读取double / float值的效果不佳,所以我将文件更改为&#34;。&#34;到&#34;,&#34; (例如: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。:你的上一次测试失败了。