Monday, September 2, 2013

First JUnit Example in Eclipse


First JUnit Example in Eclipse

Below are the Steps for the First JUnint example:
(Use Eclipse IDE)

1. Create a new Java Project "JunitBasic".
2. Add JUnit jar to your class path. (junit-4.10.jar)

3. Create a new Class "SalaryCalculation.java"

public class SalaryCalculation {

     public double overTimeCalc(int overTime, double otRate){
           return overTime * otRate;
     }

     public double incentiveCalc(double basicSalary, int noOfDays){
           double incentive = 0D;
           if(noOfDays>25)
                 incentive = ( basicSalary / 30 ) * 2.5;
           if(noOfDays>22 && noOfDays<=25)
                 incentive = ( basicSalary / 30 ) * 1.5;
                 return incentive;
     }

     public double bonusCalc(double basicSalary, double bounsPercentage){
            return basicSalary * 12 * bounsPercentage;
     }

     public double monthlySalaryCacl(double basicSalary, int noOfDays, int overTime,          double otRate, double bounsPercentage){
           return basicSalary + overTimeCalc(overTime, otRate) + incentiveCalc(basicSalary, noOfDays) + bonusCalc(basicSalary, bounsPercentage);
     }
}

4. Go to the Package Explorer / Navigator and select the class SalaryCalculation.java
Then Right click the class and Go new --> JUnit Test Case. Once you click on that it will open the below window.

5. Once you click the “next” button it will open this window. Now select what are the methods you need to perform the Unit testing and click the “Finish” button.

6. Now modify the created “SalaryCalculationTest.java” class with your test scenarios.

import junit.framework.Assert;

import org.junit.Test;

/**
* @author sanjeeva
* Assert.assertEquals(String message, double expected, double actual, double delta);
*/
public class SalaryCalculationTest {

       SalaryCalculation salaryCalculation = new SalaryCalculation();
 
       @Test
       public void testOverTimeCalc() {
             Assert.assertEquals("OverTime should be", 125, salaryCalculation.overTimeCalc(10, 12.5), 0.00);
       }

       @Test
       public void testIncentiveCalc() {
              Assert.assertEquals("When attendance grater than 25 Incentive should be", 833, Math.floor(salaryCalculation.incentiveCalc(10000, 30)), 0.00);
              Assert.assertEquals("When attendance betwee 22 to 25 Incentive should be", 500, Math.floor(salaryCalculation.incentiveCalc(10000, 25)), 0.00);
       }

       @Test
       public void testBonusCalc() {
               Assert.assertEquals("Bonus should be", 6000, salaryCalculation.bonusCalc(10000, 0.05), 0.00);
       }

       @Test
       public void testMonthlySalaryCacl() {
               Assert.assertEquals("Monthly Salary should be", 16958, Math.floor(salaryCalculation.monthlySalaryCacl(10000, 30, 10, 12.5, 0.05)), 0.00);
               Assert.assertEquals("Monthly Salary should be", 16625, Math.floor(salaryCalculation.monthlySalaryCacl(10000, 25, 10, 12.5, 0.05)), 0.00);
       }

}

7. Execute the test class by using “Junit Test” command.
a. Right click the test class.
b. Then Go : Run As → Junit Test

8. Once you run the class If all the test scenarios are passed you will get the below window. By changing the parameters you can check the scenarios got fail or pass. If the scenario got failed “Fail Tracer” will be indicate the details.