Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/unit-testing/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java JUnit中的测试数据_Java_Unit Testing_Junit - Fatal编程技术网

Java JUnit中的测试数据

Java JUnit中的测试数据,java,unit-testing,junit,Java,Unit Testing,Junit,我是测试新手。所以,我有一个学习任务 本程序计算二次方程 例如,如何检查c!=0和D>0是否使用JUnit 我想用正确的方式做这件事 我试图从JUnit调用input(),但测试变得没完没了 public class Main { public static Scanner input; public static double a, b, c; public static double d; public static double x1, x2; public static void ma

我是测试新手。所以,我有一个学习任务

本程序计算二次方程

例如,如何检查c!=0和D>0是否使用JUnit

我想用正确的方式做这件事

我试图从JUnit调用input(),但测试变得没完没了

public class Main {
public static Scanner input;
public static double a, b, c;
public static double d;
public static double x1, x2;

public static void main(String args []) {
    input();
}

public static void input() {
    System.out.println("ax^2 + bx + c = 0");
    input = new Scanner(System.in);
    try {
        System.out.print("a -> ");
        a = input.nextFloat();
        System.out.print("b -> ");
        b = input.nextFloat();
        System.out.print("c -> ");
        c = input.nextFloat();
    } catch (InputMismatchException e) {
        System.out.println("Input number!\n");
        input();
    }

    if(a == 0 || b == 0 || c == 0) {
        System.out.println("Not right\n");
        input();
        return;
    }

    calculate();
}

public static void calculate() {
    d = (b * b) - (4 * a * c);
    System.out.println("D = " + d);

    if(d < 0) {
        System.out.print("No answer");
    } else if(d == 0) {
        x1 = (-b) / (2 * a);
        System.out.println ("x = " + x1);
    } else {
        x1 = (-b + Math.sqrt(d)) / (2 * a);
        x2 = (-b - Math.sqrt(d)) / (2 * a);
        System.out.println("x1 =  " + x1);
        System.out.println("x2 = " + x2);
    }
}
公共类主{
公共静态扫描仪输入;
公共静态双a、b、c;
公共静态双d;
公共静态双x1,x2;
公共静态void main(字符串参数[]){
输入();
}
公共静态无效输入(){
System.out.println(“ax^2+bx+c=0”);
输入=新扫描仪(System.in);
试一试{
系统输出打印(“a->”);
a=输入.nextFloat();
系统输出打印(“b->”);
b=输入。nextFloat();
系统输出打印(“c->”);
c=input.nextFloat();
}捕获(输入不匹配异常e){
System.out.println(“输入编号!\n”);
输入();
}
如果(a==0 | | b==0 | | c==0){
System.out.println(“不正确\n”);
输入();
返回;
}
计算();
}
公共静态无效计算(){
d=(b*b)-(4*a*c);
System.out.println(“D=“+D”);
if(d<0){
系统输出打印(“无应答”);
}else如果(d==0){
x1=(-b)/(2*a);
System.out.println(“x=”+x1);
}否则{
x1=(-b+数学sqrt(d))/(2*a);
x2=(-b-数学sqrt(d))/(2*a);
System.out.println(“x1=”+x1);
System.out.println(“x2=”+x2);
}
}

}

您的程序在其当前形式下不可测试。这是另一种选择:

public class Parabola {
  // These are required in the constructor. Omitted for brevity
  private final double a;
  private final double b;
  private final double c;
  // Public API. Test this
  public double x1() { /*...*/ }
  public double x2() { /*...*/ }
}
现在编写测试很简单:

@Test public void test1() {
  Parabola target = new Parabola(1, 2, 3);
  assertEquals(target.x1(), 44.23);
  assertEquals(target.x2(), 17.23);
}

@Test public void test2() {
  Parabola target = new Parabola(1, 0, 0);
  assertEquals(target.x1(), -1);
  assertEquals(target.x2(), 13.43);
}
最后,这就是
main()
的外观:

public void main(String... args) throws Exception {
  Scanner console = ...;
  System.out.println("Type a:");
  Double a = Double.parseDouble(console.nextLine());
  // Same for b and c
  Parabola p = new Parabola(a, b, c);
  System.out.println("X axis intersected in "  + p.x1() + " and " + p.x2());
}

好处:在单元测试中比较
double
s时,您可能希望使用接受
阈值的参数,因为浮点运算和/或二进制数的十进制表示法是如何工作的

显示您的测试代码…测试时要做的第一件事:尝试从mix中删除用户输入。我怀疑你根本不应该使用字段;相反,您可以将所有参数直接传递给
calculate
方法。考虑到您使用的是
系统,测试
输入将更加困难。
。谢谢!但是如果我想测试输入的数据呢?那个c!=0等等?我怎么做?谢谢,您不需要测试输入数据。您测试程序是否为输入(
a1
b1
c1
)计算右
x1
x2
),然后(
a2
b2
c2
)等等。为每个测试写一个测试