使用junit

时间:2016-06-23 17:25:00

标签: java junit

我想在junit中编写计算器程序的测试用例。我是junit环境的新手。我可以为其他程序编写测试,但有点像测试switch()的情况。

我真的想知道如何去做。

提前致谢

这是我的程序

 import java.util.Scanner;

 //This program performs basic math operations such as :- +,-,*,/
  public class Calculator 

  {      
 public static void main(String[] args) 

 {
     double number1, number2;
     String Mathoperation;
     Scanner scannerObject = new Scanner(System.in);

     System.out.println("Enter first number");
     number1 = scannerObject. nextDouble();

     System.out.println("Enter second number");
     number2 = scannerObject. nextDouble();

     Scanner UserInput = new Scanner(System.in);
     System.out.println("\nHere are your options:");
     System.out.println("\n1. Addition, 2. Subtraction, 3. Divison, 4. Multiplication");
     Mathoperation = UserInput.next();

     switch (Mathoperation)  
     {
       case "1":
       System.out.println("Your answer is " + (number1 + number2));
       break;

       case "2":
       System.out.println("Your answer is " + (number1 - number2));
       break;

       case "3":
       System.out.println("Your answer is " + (number1 / number2));
       break;

       case "4":
       System.out.println("Your asnwer is " + (number1 * number2));
       break;

       default:
       System.out.println("");      
     }   
 }  

}

3 个答案:

答案 0 :(得分:1)

编写单元测试的第一步是编写可测试的代码。此时您的代码不是非常可测试的,也就是说您可以对其进行测试,但它不会成为最佳测试。让我们来看看为什么。

第一个原因是您的主要功能中包含所有代码。你通常想避免这种情况。这有多种原因,但我认为最大的两个原因是可读性和可重用性。通常你会想把东西放到不同的类中,但是为了时间,我们只需将你的数学开关放到它自己的函数中。

import java.util.Scanner;

     //This program performs basic math operations such as :- +,-,*,/
public class Calculator 

{      
 public static void main(String[] args) 

 {
   double number1, number2;
   String Mathoperation;
   Scanner scannerObject = new Scanner(System.in);

   System.out.println("Enter first number");
   number1 = scannerObject. nextDouble();

   System.out.println("Enter second number");
   number2 = scannerObject. nextDouble();

   Scanner UserInput = new Scanner(System.in);
   System.out.println("\nHere are your options:");
   System.out.println("\n1. Addition, 2. Subtraction, 3. Divison, 4. Multiplication");
   Mathoperation = UserInput.next();
   doMath(Mathoperation, number1, number2)
 }

 public double doMath(String Mathoperation, double number1, double number2){
   switch (Mathoperation)  
   {
     case "1":
     System.out.println("Your answer is " + (number1 + number2));
     break;

     case "2":
     System.out.println("Your answer is " + (number1 - number2));
     break;

     case "3":
     System.out.println("Your answer is " + (number1 / number2));
     break;

     case "4":
     System.out.println("Your asnwer is " + (number1 * number2));
     break;

     default:
     System.out.println("");      
   }   
 }
}

好的很酷,所以现在我们有自己的方法来进行数学运算。一个问题。这本身并不是一个单元,这实际上是加法和数学运算之间的整合。这意味着,测试时,我们需要测试算术的每个组合(加,子,除,乘)以及方法是否选择正确的算子。那就是我们想要的更多工作,并不是一个非常好的测试,所以我们如何解决这个问题呢?很简单,我们进一步细分

import java.util.Scanner;

     //This program performs basic math operations such as :- +,-,*,/
public class Calculator {
//Code for use input somewhere here      
 public double doMath(String Mathoperation, double number1, double number2){
   switch (Mathoperation)  
   {
     case "1":
     System.out.println("Your answer is " + add(number1, number2));
     break;

     case "2":
     System.out.println("Your answer is " + sub(number1, number2));
     break;

     case "3":
     System.out.println("Your answer is " + div(number1, number2));
     break;

     case "4":
     System.out.println("Your answer is " + mul(number1, number2));
     break;

     default:
     System.out.println("");      
   }   
 }
 public double add(double number1, double number2){
   return number1 + number2;
 }
 public double sub(double number1, double number2){
   return number1 - number2;
 }
 public double mul(double number1, double number2){
   return number1 * number2;
 }
 public double div(double number1, double number2){
   return number1 / number2;
 }
}

现在我们的单位更容易分解。在这里,您可以为addsubdivmul函数编写测试。对它们进行测试以确保它们按预期运行。然后编写doMath函数的测试,以确保它将值传递给正确的操作。通过这种方式,当某些事情失败时,你就会知道它失败的确切位置以及问题所在,反对必须猜测。您将知道doMath是否存在问题,或者您的其他数学方法是否存在问题。它还允许您更轻松地重用您的方法用于其他事情,因此双赢

答案 1 :(得分:1)

考虑将输入,计算和输出放入(非静态)方法,这将使逻辑测试成为可能:

//This program performs basic math operations such as :- +,-,*,/
public class Calculator {

    double number1, number2;
    String Mathoperation;

    Calculator()

    {

        //you need to remove input and calculation logic from constructor,
        //to avoid running it when constructing this object for testing.
    }

    public void run() {

        getInput();
        double result = doMath(Mathoperation, number1, number2 );

        System.out.println("Your answer is " + result );
    }

    private void getInput() {

        Scanner scannerObject = new Scanner(System.in);

        System.out.println("Enter first number");
        number1 = scannerObject. nextDouble();

        System.out.println("Enter second number");
        number2 = scannerObject. nextDouble();

        Scanner UserInput = new Scanner(System.in);
        System.out.println("\nHere are your options:");
        System.out.println("\n1. Addition, 2. Subtraction, 3. Divison, 4. Multiplication");
        Mathoperation = UserInput.next();
    }

    /**
     *@param mathoperation
     */
    public double  doMath(String mathoperation, double number1, double number2) {

        switch (mathoperation)
        {
        case "1":
            return (number1 + number2);

        case "2":
            return  (number1 - number2);

        case "3":
            return (number1 / number2);

        case "4":
            return (number1 * number2);

        default:
            throw new IllegalStateException("mathoperation is not valid");
        }
    }

    public static void main(String[] args)

    {
        Calculator calc = new  Calculator();
        calc.run();
    }
}

这是一个非常简单的JUnit测试示例:

public class CalculatorTest {

    /**
     * Test method for {@link Calculator#doMath(java.lang.String, double, double)}.
     */
    @Test
    public void testDoMath() {

        Calculator calc = new Calculator();

        assertTrue(calc.doMath("1", 5, 7) == 12.0);
        assertTrue(calc.doMath("2", 5, 7) == -2.0);
        assertTrue(calc.doMath("3", 5, 7) == (5.0/7.0));
        assertTrue(calc.doMath("4", 5, 7) == (5.0*7.0));
    }

}

答案 2 :(得分:0)

要编写单元测试,您通常必须拥有要测试的API部分。在您的情况下,一切都在收集用户输入的主要方法中,因此很难测试。您应该提取类似于返回答案的double performOperation(number, number, operation)方法。然后,您可以使用此方法的不同输入编写JUnit测试。