Subscribe

RSS Feed (xml)

Powered By

Skin Design:
Free Blogger Skins

Powered by Blogger

Thursday, August 20, 2009

JUnit Tests

Hi Friends,
Today Lets see A sample program as to how JUnit test is performed.
For readers who can grasp information if explained using a program, this post is going to be very Informative.

First We Have A Class for which JUnit has to be written

// JUnit test example

public class Rectangle
{
// State variables.
private int height;
private int width;

// Constructors
Rectangle()
{
height=0;
width =0;
}
Rectangle(int h,int w)
{
height=h;
width =w;
}

// Readers
public int getH()
{return height;}
public int getW()
{return width;}
public int getA()
{return height* width;}

// Writers
public void setH(int h)
{height=h;}
public void setW(int w)
{width =w;}

public String toString()
{return "Rectangle: height="+height+"; width="+ width +
"; area="+getA()+".";}

} // End of Rectangle class

Now We Create A TestCase for The Rectangle Class





import static org.junit.Assert.*;
import org.junit.*;

public class RectangleTest extends TestCase
{
Rectangle r;
Rectangle[] rList=new Rectangle[5];

@Before
// Will be performed before each test.
public void testSetup()
{
System.out.println("Setup for test complete.");
}

@After
// Will be performed after each test.
public void testComplete()
{
System.out.println("Test complete.");
}

@Test
public void test1()
{
// Can be run when reader methods and default
// constructor are complete.
r=new Rectangle();
try
{
assertTrue("Test 1: Default values are wrong.",
r.getH()==0 && r.getW()==0);
System.out.println("Test 1 completed successfully.");
}
catch (AssertionError e)
{
System.out.println(e.getMessage());
}
}

@Test
public void test2()
{
// After area reader is written.
r=new Rectangle();
try
{
assertTrue("Test 2: Default values are wrong.",
r.getH()==0 && r.getW()==0 && r.getA()==0);
System.out.println("Test 2 completed successfully.");
}
catch (AssertionError e)
{
System.out.println(e.getMessage());
}
}



@Test
public void test3()
{
// After second constructor is written.
r=new Rectangle();
try
{
r=new Rectangle(10,20);
assertTrue("Test 3: Initial values are wrong.",
r.getH()==10 && r.getW()==20 && r.getA()==200);
System.out.println("Test 3 completed successfully.");
}
catch (AssertionError e)
{
System.out.println(e.getMessage());
}
}

@Test
public void test4()
{
// After second constructor is written.
r=new Rectangle();
try
{
for (int i=0;i<5;i++)
{
rList[i] = new Rectangle(2*i,3*i);
assertTrue("Test 4: Initial values are wrong.",
rList[i].getH()==2*i &&
rList[i].getW()==3*i &&
rList[i].getA()==6*i*i);
}
System.out.println("Test 4 completed successfully.");
}
catch (AssertionError e)
{
System.out.println(e.getMessage());
}
}

@Test
public void test5()
{
// After writers are written.
r=new Rectangle();
try
{
for (int i=0;i<5;i++)
{
rList[i] = new Rectangle(2*i,3*i);
rList[i].setH(11*i);
rList[i].setW(7*i);
assertTrue("Test 5: Modified values are wrong.",
rList[i].getH()==11*i &&
rList[i].getW()==7*i &&
rList[i].getA()==77*i*i);
}
System.out.println("Test 5 completed successfully.");
}
catch (AssertionError e)
{
System.out.println(e.getMessage());
}
}

@Test
public void test6()
{
// toString test.
r=new Rectangle();
try
{
for (int i=0;i<5;i++)
{
rList[i] = new Rectangle(2*i,3*i);
rList[i].setH(11*i);
rList[i].setW(7*i);
assertTrue("Test 6: Modified values are wrong.",
rList[i].getH()==11*i &&
rList[i].getW()==7*i &&
rList[i].getA()==77*i*i);
System.out.println(rList[i]);
}
System.out.println("Test 6 completed successfully.");
}
catch (AssertionError e)
{
System.out.println(e.getMessage());
}
}

public static void main(String[] args)
{
org.junit.runner.JUnitCore.main("RectangleTest");
}
}

Output from correct Rectangle class test:

Setup for test complete.
Test 1 completed successfully.
Test complete.
Setup for test complete.
Test 2 completed successfully.
Test complete.
Setup for test complete.
Test 3 completed successfully.
Test complete.
Setup for test complete.
Test 4 completed successfully.
Test complete.
Setup for test complete.
Test 5 completed successfully.
Test complete.
Setup for test complete.
Rectangle: height=0; width=0; area=0.
Rectangle: height=11; width=7; area=77.
Rectangle: height=22; width=14; area=308.
Rectangle: height=33; width=21; area=693.
Rectangle: height=44; width=28; area=1232.
Test 6 completed successfully.
Test complete.


I changed setH() so that height was set to h*2 and reran the tests.

Setup for test complete.
Test 1 completed successfully.
Test complete.
Setup for test complete.
Test 2 completed successfully.
Test complete.
Setup for test complete.
Test 3 completed successfully.
Test complete.
Setup for test complete.
Test 4 completed successfully.
Test complete.
Setup for test complete.
Test 5: Modified values are wrong.
Test complete.
Setup for test complete.
Rectangle: height=0; width=0; area=0.
Test 6: Modified values are wrong.
Test complete.

No comments: