Thursday, June 11, 2009

JUnit on myeclipse


JUnit on myeclipse

by Satya Siripuram





What is Junit


JUnit is a simple open source Java testing framework used to write and run repeatable automated

tests. It is an instance of the xUnit architecture for unit testing framework. Eclipse supports

creating test cases and running test suites, so it is easy to use for your Java applications.

JUnit features include:


Assertions for testing expected results

Test fixtures for sharing common test data

Test suites for easily organizing and running tests

Graphical and textual test runners


What is a test case


A test case is a class which holds a number of test methods. For example if you want to test some

methods of a class AddClass you create a class AddClassTest which extends the JUnit TestCase class and place your test methods in there.




Working with Junit on myeclipse:


Step 1: select Window → Show View → Other → Java → select Junit → Ok


Just beside Package Explorer tree you could find JUnit view.



Example application:


1. Create a project :

a. go to File → New → Other → Java Project

b. name the project as TestingJUnitFinish

    1. Create a simple class:

a. right click on the project TestingJUnit goto New → Class

b. give package name as test.junit, class name as AddClass → Finish


AddClass.java


package test.junit;

public class AddClass {

public int add(int c ,int d)

{

return c+d;

}

}


3. Adding junit.jar to the project:

a. right click on the project → Properties → Java Build Path → select Libraries tab → Add External Jars → select the junit.jar → Ok


Note: download junit.jar here http://sourceforge.net/project/showfiles.php?group_id=15278&package_id=12472


  1. Creating test cases :

a. right click on the project TestingJUnit → New → Other → select Junit from the tree → Junit Test Case

b. give the package name as test.junit , class name as AddClassTest → only check setUP().

c. click on Browse for Class Under Select and type the name of the class that you want to test , here AddClass → Finish

d. add the following line of code inside testAdd()


assertEquals(13, ac.add(6, 7));


13 is the expected value.

ac.add(6,7) is the actual value



AddClassTest.java


package test.junit;


import junit.framework.TestCase;


public class AddClassTest extends TestCase {

private AddClass ac;


protected void setUp() throws Exception {

ac = new AddClass();

}


public void testAdd() {


assertEquals(13, ac.add(6, 7));


}


}


5. Running the Test Cases :

a. Right click on the project TestingJUnit → Run As → JUnit test





on Junit view you could see the following.




for testing give some different value like


assertEquals(12, ac.add(6, 7));


and Run again now you could see the following.






This is how you can test your application.


References:


http://www.scribd.com/doc/18134/Eclipse-tutorial-part-10-Using-JUnit-in-Eclipse


thanks and please send your comments to sat4java@gmail.com



Bookmark this pageDigg this sitesimpyslashdot