Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

testing program with switch case using junit

Tags:

java

junit

I want to write test case for calculator program in junit. I am new to junit environment. I can write tests for other programs but kind of stuck with testing for switch() case.

I really want to know how to to do it.

thanks in advance

here is my program

 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("");      
     }   
 }  

}
like image 249
s.p Avatar asked Dec 14 '25 15:12

s.p


1 Answers

The first step to writing unit tests is writing testable code. At this point your code isn't very testable, which is to say you can test it but it won't be the best tests. Lets have a look at why.

The first reason is that you have all your code in your main function. You usually want to avoid this. There are multiple reasons for this, but the two biggest in my opinion are readability and reusability. Normally you would want to put things into different classes, but for the sake of time we will simply put your math switch into its own function.

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("");      
   }   
 }
}

Ok cool, so now we have our own method for doing our mathematic operations. One issue tho. This isn't a unit in of itself, this is actually an integration between addition and our math operation. What this means is, to test, we would need to test each combination of arithmetic (add, sub, divide, multiply) as well as if the method selects the right operator. Thats alot more work then we want, and isn't a very good test, so how do we fix this? Well simple, we break it down further

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;
 }
}

Now our units are broken down much more easily. From here you would write test for your add, sub, div, and mul functions. Test these out well to insure that they function as expected. Then write tests for the doMath function to make sure it passes the values to the right operation. In this way when something fails you know exactly where its failing and what the issue is, as oppose to having to guess. You will know if doMath has an issue, or if one of your other math methods is the problem. It also allows you to more easily reuse your methods for other things, so win-win

like image 84
Mo H. Avatar answered Dec 17 '25 03:12

Mo H.



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!