JUnit is using for unit testing.
Note that this is not a replacement to the Testing phase, which is a part of the Software Development Life Cycle. (SDLC)
This is normally use for test programms, from the programmers perspective.
If u can do testing eith JUnit u can definitly improve the quality of your source-code. (So, less # of errors will be getting from the regular testing phase)
This is an sample example:
Assume this is a small program of doing calculations,
(This is what a programmer write normally)
public class Cal{
public int add(int a,int b){
return a+b;
}
public int sub(int a,int b){
return a-b;
}
public int mult(int a,int b){
return a*b;
}
public int div(int a,int b){
return a/b;
}
}
When use JUnit, the test-code will look like this;
(This should write in addition to the Cal.java)
import junit.framework.TestCase;
public class CalTest extends TestCase{
private Cal cal;
//initialize the fixture
public void setUp(){
cal = new Cal();
}
//clean up
public void tearDown(){
//do clean ups
}
//test method for add
public void testAdd(){
int x=4,y=6;
int actual = cal.add(x,y);
int expected = 10;
assertEquals(expected,actual);
}
//test method for sub
public void testSub(){
int x=4,y=6;
int actual = cal.sub(x,y);
int expected = -2;
assertEquals(expected,actual);
}
//test method for mult
public void testMult(){
int x=4,y=6;
int actual = cal.mult(x,y);
int expected = 24;
assertEquals(expected,actual);
}
//test method for div
public void testDiv(){
int x=6,y=3;
int actual = cal.div(x,y);
int expected = 2;
assertEquals(expected,actual);
}
This is an additional overhead to the programmer... but sometimes it may worth of doing it
Visit www.junit.org