JUNIT Projects

JUNIT Project 1

JUNIT Examples


The JUnit Main() Testing:
Previous Home Next

In any Java Program the java developer create a main method, and the main class to create a instance of the main class in method, create a main method that instantiates an instance of the class.


In main method define the class HtmlDocument for create a object.

public static void main(String args[]) throws Exception {
HtmlDocument doc = new HtmlDocument(new File(args[0]));
System.out.println("Title = " + doc.getTitle());
System.out.println("Body = " + doc.getBodyText());
The Creating the Java Program Calculator:

This class has sum method which takes two int parameters to add them and return the value for this variable.

package R4R;
public class Calculator 
int sum(int i, int j)
return i+j;

Creating CalculatorTest.java:

If you create a CalculatorTest class and to test that method sum() is working fine or not you check it. It means you creating another class and this class named CalculatorTest. then this file Save and compile this file. Before processing we should first have a look over Junit coding convention.

The Coding Convention:
  1. Name of the test class is must end with Test.
  2. The Name of the method must begin with test.
  3. Return type of a test method must be void.
  4. Test method must not throw any exception.
  5. Test method must not have any parameter.

In this program the class name which we are going to test is Calculator , when you want to test your Java program Calculator. You have created class CalculatorTest here. In this program in the same way the method created to test the particular method will be appended with test word as in the example we are going to test the method sum() hence we have created method testSum() in this class.

package R4R;
import junit.framework.TestCase;
public class CalculatorTest extends TestCase 
Calculator cal=new Calculator();  
public CalculatorTest(String name) 
public void testSum() {

The Testing For our program Explanation:
  1. pakage R4R
  2. Firstly create the package for any Program. and mention the Name of the package.

  3. import junit.framework.TestCase;
  4. Actually junit is a testing framework. And we need to use some classes of JUnit constructs in the testing program so we have to use import the framework file.

  5. public class CalculatorTest extends TestCase
  6. If you want to define own test method then define into TestCase class in our own testing class. In this Program we are testing for adding two numbers. then created another class is named by CalculatorTest. then this class is extends for the TestCase class.

  7. public CalculatorTest(String name) { super(name); }
  8. When we test the functionality then we can see the output to check which test has produced error i.e. which test fails. So for this every test is given name. The constructor of the class provides this functionality by passing this parameter to the constructor of the parent class.

  9. public void testSum() assertEquals(2,cal.sum(1,1));
  10. The method testSum|() is how to test the functionality of the adding two numbers in the Calculator.java class.We can also define variables and perform arithmetic calculations just as we do in any Java program. In this method we are checking whether the value returned by sum method of the object of the Calculator class is equal to 2. If it is so then the test will be shown successful. If the method did not perform as expected then it will cause assertEquals() to fail. Now we need to fix the problem and run the test again . We need to repeat this process until the test is passed.

Previous Home Next