1) Download JUnit jar library from JUnit website [http://www.junit.org/]
2) Add external library to your Eclipse Project
3) Using JUnit API assert existing implementation by creating test case
import junit.framework.TestCase;
public class EmpolyeeHandlerTest extends TestCase {
public void testGetAnnualPerks() {
EmployeeHandler handler
= new EmployeeHandler();
//assertEquals(expected Value, Actual Value);
assertEquals(400.0,
handler.getAnnualPerks(1));
assertEquals(900.0,
handler.getAnnualPerks(2));
assertEquals(1600.0,
handler.getAnnualPerks(3));
assertEquals(3000.0,
handler.getAnnualPerks(4));
}
}
4) Existing implementation which is to be unit tested with above test class
public class EmployeeHandler {
public double getAnnualPerks(int empGrade) {
int salary = 0; // local variable
double perks = 0; // local variable
switch (empGrade) {
case 1 :
salary
= 4000;
perks
= salary * 0.10;
break;
case 2 :
salary
= 6000;
perks
= salary * 0.15;
break;
case 3 :
salary
= 8000;
perks
= salary * 0.20;
break;
case 4 :
salary
= 10000;
perks
= salary * 0.30;
break;
default :
System.out.println("Invalid Employee grade.");
}
return perks;
}