How we will provide Junit Test to our application?
>> Thursday, May 26, 2011
- If a user is testing the functionality which was written that we have called as "UnitTesting.".
- If a programmer is testing the code that was written other programmer it is called "Peertesting"
- Generally we will use Junit for UnitTesting.
1.)Develop our main class
public class Calculate
{
public int AddNumber(int i,int j){
return i+j;
}
2.Develop a test case for this class
Note:Our test case class must extends Junit.framework.TesrcaseTestcase class of Junit framework is having 2 life cycle methods and some Testxxx() methods.
Note:setup(), tearDown() are called life cycle methods of Junit.
setUp():It is used for instanciation of requried resources.It is like init() .
tearDown(): It is for releasing the resources .It is similar to destroy() of servlet.
In testxxx() method we will write our logic for testing the functionality.
- we will use fail(string s) methos to show & terminate the method.
- We will write the assertxxx() method to test the logic.
- We can also use the tail() in the catch().
- We will write the assertxxx() methods in the try block.
Sample Program:
public clas MultipleTestCase extend TestCase{
public Calculate objcalculate=null;
protected void setUp() throws Exception(){
Super.setUp();
objcalculate=new Calculate();
}
protected void tearDown() throws Exception(){
Super.tearDown();
objcalculate=null;
}
public void test multiplicationTesting(int i,int j) {
int k=10;
int p=30;
if(k==0|| p==20)
{
tail("zero is not accepted")
}
assertEquals(100,objCalculate.multipleNumber(3,6))
System.println("After satisfing the assert equal")
}
}
0 comments:
Post a Comment